Search the Community
Showing results for tags 'model'.
-
Larry, on page 150 of the YiiBook2 (latest version), you write that the following code is "verbose, redundant, and illogical": $model = new Page(); $model = $model->findOne($id); Instead of creating an instance of the Page class, as shown above, you say we should use a static class instance (or static method?), like this: if (($model = Page::findOne($id)) !== null) { return $model; etc. I don't understand why the first example is "verbose, redundant, and illogical", and why the second example is preferred. Can you please explain this? Thanks.
-
- static class instance
- static class
-
(and 5 more)
Tagged with:
-
Hi, I started reading the chapter about Document Object Model, and I have a few beginner questions: 1. Quote from book: Isn't HTML element the root node? 2. Quote from book: But on http://www.w3schools.com/jsref/prop_node_nodetype.asp and https://developer.mozilla.org/en-US/docs/Web/API/Node.nodeType this is different: Also, what is the difference between "1 for an HTML element" and "10 for the HTML element"? I guess that's a mistake. Thank you in advance!
-
Dear all, I follow the yii book until chapter 5. I got no problem until I reach page 94 and I add this code: # protected/models/Page.php::rules() array('date_published', 'date', 'format'=>'YYYY-MM-DD'), I got error while opening create Page: Please fix the following input errors: The format of Date Published is invalid. When I type 2013-04-05. Is there something wrong?
-
Version 0.5 Loading a Single Model The following two examples work together. The second calls the first. I believe the purpose is to retrieve a record from a table. And I believe each occurrence of $id represents the primary key value of the particular record we want. My question is, how do these functions know which table to search in? page 136: public function loadModel($id) { $model = Page::model()->findByPk($id); if ($model===null) { throw new CHttpException(404, 'The requested page does not exist.'); } Page 139: public function actionView($id){ $this->render('view',array( 'model'=>$this->loadModel($id), )); }
-
Hi, I need to use a model with dynamic table name in my application. I have found a piece of code to use like that: $model = new DynamicModel($tableName); The code is as follows: class DynamicModel extends CActiveRecord { private static $_tableName; public function __construct($tableName) { if(strlen($tableName) == 0) { return false; } if(strlen($tableName)>0){ self::$_tableName = $tableName; } self::setIsNewRecord(true); } public static function model($tableName) { if(strlen($tableName) == 0) { return false; } $className=__CLASS__; if(strlen($tableName)>0){ self::$_tableName = $tableName; } return parent::model($className); } public function tableName() { return '{{'.self::$_tableName.'}}'; } public function setTableName($tableName) { self::$_tableName = $tableName; } ...// The rest of model code }
-
Hi, I was hoping someone might have some experience using model inheritance in Yii. So far, I've only seen single table inheritance solutions (a la http://www.yiiframework.com/forum/index.php/topic/12775-unified-relational-model-into-one/page__p__63052#entry63052 and http://www.yiiframework.com/forum/index.php/topic/12978-class-table-inheritance/page__p__135262#entry135262). In practice, my requirements are only for a single level of inheritance. But in principle, the method prompts reservations. Before deciding on Yii as the framework, I spent a significant amount of time deciding on the database schema. One of my drafts included an arrangement that would support the single table inheritance solution with a 'type' attribute. However, I realized the 'correct' approach would be to just use JOINs, joining a table of an inheriting table to the table of a parent class (e.g. join the 'student' and 'parent' tables with the 'user' table on a primary/foreign key relationship). Most bothersome of the single table inheritance solution is the extraneous 'type' key which is not only unnecessary but also limiting to...single table inheritance. Is the concensus that the solution I'm looking for just not possible with Yii and I have to accept single table inheritance as the best answer? Can anyone share their experiences that will prove me wrong? Let me know if I can be any clearer or provide more examples. Any help is appreciated. Thanks, Michael