Jump to content
Larry Ullman's Book Forums


  • Content Count

  • Joined

  • Last visited

  • Days Won


Everything posted by lrzins

  1. Hi Larry, on page 216 of your Yii book, you describe how to rename an uploaded file using the $model->id of the User model. I think this is a good idea, but I don't see how your code can work without an afterSave or something. Here is your code: $model->avatar->saveAs($dest . '/' . $model->id . '.' . $model->avatar->extensionName); The line above would have to come after the code to save the model: if ($model->save()) because you don't obtain the $model->id until after you save it. But since you've already saved the file's name to the database, the filen
  2. Hi Larry, to determine if any records are returned using find() and findAll(), I see that when nothing is returned using find(), that a NULL is returned. When nothing is found using findAll that an empty array is returned. Is this the best way to determine if records are returned?: $var=SomeModelClass::model()->find($condition, $params); if ($var !== null) // Check if a single record is returned. $var=SomeModelClass::model()->findAll($condition, $params); if (!empty($var)) // Check for returned records Larry Z.
  3. Wow!!!!! I got it working!!!! Iiiiieeeeeeeeyyyaaaaa!!! I just had to change the 'desc' value (above) to this: 'desc'=>'department.department_abbreviation desc, course_number desc', and now it finally sorts with two related fields. Happy happy Larry Z.
  4. Update: To sort by department abbreviation + course number, I had to use this in the Course model: public function search() { $criteria=new CDbCriteria; $criteria->with = array('department'); $criteria->compare('department.department_abbreviation',$this->dept_course,true); // Other criteria return new CActiveDataProvider($this, array( 'criteria'=>$criteria, 'sort'=>array( 'attributes'=>array(
  5. Hi Larry, I got the column to sort. I'm fairly comfortable with the solution, but still wonder if there is a better way. I'll definitely revisit this later. Here's what I did: Created a new attribute called $dept_course in my Model: class Course extends CActiveRecord { public $dept_course; Then set up the search() method in the Course Model to use this attribute to access the related fields in the Department Model, and sort the returned values: public function search() { $criteria=new CDbCriteria; $criteria->with = array('department
  6. Well in fact that column is defined as: array('name'=>'Course', 'value'=>'CValidator::isEmpty($data->course) ? "Other University" : $data->course->department->department_abbreviation . " " . $data->course->course_number'), but now it's not sortable when clicking the column header. I can't figure out how to sort this column. Is it easy? Larry Z.
  7. Hey this works: array('name'=>'Course', 'value'=>'CValidator::isEmpty($data->course) ? " " : $data->course->course_number'), Yay! Larry Z.
  8. I found the cause of the problem. It's that I need to have a NULL value in a foreign key. I have the foreign key "course_id" defined in a course_taken table, that references the course table, that needs to be NULL sometimes. In the CGridView widget, with a NULL value for "course_id" this way works: 'course.course_number', and this way generates the error "Trying to get property of non-object": array('name'=>'Course', 'value'=>'$data->course->course_number'), So I believe the best solution is to check for a NULL in the 'value' property (above). Unfortunately this level of
  9. Sorry to spam the list again. I just deleted all rows from the course_taken table, and it works now: array('name'=>'Course', 'value'=>'$data->course->department->department_abbreviation . " " . $data->course->course_number'), I did'nt even look at the data first, before deleting it, so it's not clear what the cause was. Onward! Larry Z.
  10. Hi there, I've got a column defined in a CGridView widget, in the view file admin.php, that correctly returns the course_number attribute from a related table: <?php $this->widget('zii.widgets.grid.CGridView', array( 'id'=>'course-taken-grid', 'dataProvider'=>$model->search(), 'columns'=>array( 'course.course_number', // Other columns array( 'class'=>'CButtonColumn', ), ), )); ?> For some reason, if I use the name/value syntax to do the same thing: array('name'=>
  11. Oh, it turns out that the _view.php file is only used by the index.php file. Sorry if that mislead/confused anybody. Larry Z.
  12. For Pete's sake, I just figured it out, thanks to this post: http://www.yiiframework.com/forum/index.php/topic/28254-using-cdetailview-to-show-another-models-view/ and then this made more sense: http://www.yiiframework.com/doc/api/1.1/CDetailView#attributes-detail At least this is one way to do it I suppose. You can access data in the database directly using a relation defined in your model. In my Course model I have this relation: 'department' => array(self::BELONGS_TO, 'Department', 'department_id'), In the view.php file I add an attribute to the CDetailView w
  13. Hi, I'm using Yii version 1.1.13. I've generated a variable in a controller, that I need to display in the view file "_view.php". I pass the variable to "view.php" with render, and I can access it there. But I can't figure out how to access the variable from "_view.php", which uses the CDetailView widget. How is this done? Am I missing something? You would think this would be very easy, since it is such a common task. Thanks for any tips, Larry Z.
  14. Hi, I am having to use the same Model methods in several different views. For example, in several different views for creating a new record, I have forms that have a drop down list of years from which to choose. I can add the same method getYears() to each model, and call the method from _form.php, like so: <?php echo $form->dropDownList($model, 'year_entered', $model->getYears(), array('empty' => 'Select Year')); ?> Or, I can reuse that method from another view like so: <?php echo $form->dropDownList($model, 'year', OtherModel::getYears(), array('empty'
  15. Thanks Edward and Antonio, that explains it. As Edward suggested, I moved the code from the controller into the model, and now have: In models/StudentRecord.php public function getStudentNames() { return CHtml::listData(User::model()->with('studentRecords')->findAll(), 'id', 'FullName'); } In models/User.php public function getFullName() { return $this->first_name . " " . $this->last_name; } And in views/studentRecord/_form.php <?php echo $form->dropDownList($model, 'user_id', $model->getStudentNames(), array('empty' => 'Sele
  16. Hi, I've created a drop down list of user's names, that I've concatenated from first_name and last_name in a database table. And it works perfectly! Here's what I've got: User Model: public function getFullName() { return $this->first_name . " " . $this->last_name; } StudentRecordController, in actionCreate(): $students = CHtml::listData(User::model()->with('studentRecords')->findAll(), 'id', 'FullName'); And in views/studentRecord/_form.php: <?php echo $form->dropDownList($model, 'user_id', $students, array('empty' => 'Select Student
  17. Just to wrap this up, I recreated the database tables with MyISAM, and used the "comment" method to declare the table relationships, and Gii outputed the exact same model files (rules and relations). I've used other PHP frameworks before, and Yii is looking very good. I'm delighted that Larry Ullman is writing a book about it. He's the best. In the future, I'll try not to use this forum as a personal blog (but hopefully it was useful to somebody). Larry Z.
  18. After looking into the problem more, I see now that in this relationship, the faculty table is the parent table, and the student_record table is the child table, and with a foreign key constraint, you can't insert a record in a child table without a reference to the parent table. So yes I have to remove the constraint. The lesson learned here, is that you can't liberally apply foreign key constraints just so that Gii will generate the table relationships in the model files for you! I will now try using the comment method of specifying relationships in MyISAM tables for Gii: approved_by
  19. Hi, I have a student_record table that has a field "approved_by" that has a BELONGS_TO relationship to a faculty table. My intention is to be able to create a student record for a student, and then at some point in the future, one of a list of faculty members can approve something. So I have the following foreign key contraint set up on the student_record table: CONSTRAINT FOREIGN KEY (approved_by) REFERENCES faculty (id) ON DELETE SET NULL ON UPDATE NO ACTION And I find that if I try to create a new student record, it is rejected with an "Integrity const
  • Create New...