background preloader

通用方法

Facebook Twitter

Drills : Search by a HAS_MANY relation. Sometimes we get lost trying to search by a HAS_MANY relation using CActiveRecord or CActiveDataProvider. This article is a series of drills that try to describe the practical techniques of searching by a HAS_MANY relation. Relation ¶ Two entities are sometimes connected with a relation of 1:N. Or we may say that 1:N is the only possible relation between 2 entities as long as we are in the RDB world. 1:1 relation is just a particular kind of 1:N where N is always assumed to be 1 at the maximum. And N:N relation can be considered as a combination of two 1:N relations. Yii supports this 1:N relation in CActiveRecord as BELONGS_TO and HAS_MANY relations. 1:N relation seen from the side of N is BELONG_TO, and from the side of 1 it is HAS_MANY.

BELONGS_TO relation is fairly easy. Now, let's construct an example of 1:N relation. Example of HAS_MANY ¶ Think about blog posts and their authors. We are going to solve the possible use cases of search regarding this example. Task #1 ¶ Task #2 ¶ ... Huh? Relations: BELONGS_TO versus HAS_ONE. It's very common to see new Yii users confusing the relations HAS_ONE and BELONGS_TO, and getting it wrong means you won't get proper values back. And though we'll talk about HAS_MANY as well, we're specifically omitting the MANY_MANY relation because it's a whole different animal. Both BELONGS_TO and HAS_ONE are about linking two models together, and sound like the same thing, but they link in essentially opposite directions.

Let's illustrate with three simple tables, each of which has a primary key (id), and a number of linking fields (user_id) that reference the User table. USER table - id - name POST table - id - user_id REFERENCES User.id - content PROFILE table - id - user_id REFERENCES User.id - profile_info KEY POINT: A BELONGS_TO relation says that a field in this model points to the primary key in another model; in this case, the current model owns the linking field.

Given that, a CHILD BELONGS_TO a PARENT and a PARENT HAS_ONE CHILD. 1. Weird example 1. Some points to remember: Working with Databases: Relational Active Record. We have already seen how to use Active Record (AR) to select data from a single database table. In this section, we describe how to use AR to join several related database tables and bring back the joint data set. In order to use relational AR, it is recommended that primary-foreign key constraints are declared for tables that need to be joined. The constraints will help to keep the consistency and integrity of the relational data. For simplicity, we will use the database schema shown in the following entity-relationship (ER) diagram to illustrate examples in this section. ER Diagram Info: Support for foreign key constraints varies in different DBMS. 1. Before we use AR to perform relational query, we need to let AR know how one AR class is related with another.

Relationship between two AR classes is directly associated with the relationship between the database tables represented by the AR classes. Declaring relationship in AR involves overriding the relations() method of CActiveRecord. Or. Reference: Model rules validation. This is a reference to be used for Model rule validation and is compiled from the Yii documentation and code. The purpose is to have all the information gathered in one place instead of scattered. This reference is not an intro.

See The Definitive Guide to Yii, Declaring Validation Rules for a tutorial. How validation works ¶ The CModel class uses a method named CModel::rules() to return an array with the rules for validation. public function rules(){ return array( array('username, password', 'required'), array('password_repeat', 'required', 'on'=>'register'), array('password', 'compare', 'compareAttribute'=>'password_repeat', 'on'=>'register'), ); } The code above is an example of what the CModel::rules() function may look like.

These rules are applied by the CModel::validate() method which returns a boolean value. Parameters of a validator ¶ array( 'attribute list', 'validator name', 'on'=>'scenario name', 'message'=>'The attribute didn\'t validate! ' Choice of validators ¶ Scenarios ¶ Working with Forms: Creating Model. Before writing the HTML code needed by a form, we should decide what kind of data we are expecting from end users and what rules these data should comply with. A model class can be used to record these information. A model, as defined in the Model subsection, is the central place for keeping user inputs and validating them. Depending on how we make use of the user input, we can create two types of model.

If the user input is collected, used and then discarded, we would create a form model; if the user input is collected and saved into database, we would use an active record instead. Note: We are mainly using form models in the examples of this section. 1. Below we create a LoginForm model class used to collect user input on a login page. Class LoginForm extends CFormModel{ public $username; public $password; public $rememberMe=false; } Three attributes are declared in LoginForm: $username, $password and $rememberMe. 2. Each rule returned by rules() must be of the following format: 3. 4.