primaryKey.
If there is not any primary key in the table, for example: `id` int(11) NOT NULL AUTO_INCREMENT + PRIMARY KEY (`id`), so we can redefine the primaryKey() method in the model.
For instance: there is a `users_tel_nums` table with the following structure:
- CREATE TABLE IF NOT EXISTS `users_tel_nums` (
- `userId` int(11) NOT NULL COMMENT 'user ID',
- `number` varchar(20) NOT NULL COMMENT 'Phone number',
- KEY `userId` (`userId`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
If only one phone number of a user is stored in the table, so the primaryKey method will look like:
- public function primaryKey() {
- return 'userId';
- }
But if ona and the same user can have some phone numbers, we should use a composite primary key, and the primaryKey method will look like:
- public function primaryKey() {
- return array('userId', 'number');
- }
Search by a HAS_MANY relation.
We have an ‘Objects’ model (the list of objects), ‘Users’ (the list of users) and ‘UsersTelNums’ (the list of users’ phone numbers). With the appropriate table names.
The structure of `objects` table is the following:
- CREATE TABLE IF NOT EXISTS `objects` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `status` tinyint(1) NOT NULL COMMENT 'Status',
- ....
- `ownerId` int(11) NOT NULL COMMENT 'ID of object's owner',
- ...
- PRIMARY KEY (`id`),
- KEY `ownerId` (`ownerId`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Here is the ‘users’ table:
- CREATE TABLE IF NOT EXISTS `users` (
- `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
- `firstName` varchar(64) NOT NULL,
- ...
- PRIMARY KEY (`id`),
- UNIQUE KEY `email` (`email`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
And here is the ‘users_tel_nums’ table:
- CREATE TABLE IF NOT EXISTS `users_tel_nums` (
- `userId` int(11) NOT NULL COMMENT 'user ID',
- `number` varchar(20) NOT NULL COMMENT 'Phone number',
- KEY `userId` (`userId`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
In the ‘UsersTelNums’ model (a ‘users_tel_nums’ table) a composite key is used in the ‘primaryKey’ method. We have described it above.
Here is the ‘relations’ method of the ‘Objects’ model:
- public function relations() {
- return array(
- ...
- 'user' => array(self::BELONGS_TO, 'Users', 'ownerId'),
- 'usersTelNums' => array(self::HAS_MANY, 'UsersTelNums', array('userId' => 'ownerId')),
- );
- }
Here the ‘objects’ table relates via a ‘ownerId’ field to a primary key (id) of the ‘users’ table. And the ‘objects’ table relates via a field ‘ownerId’ to ‘userId (array('userId' => 'ownerId'))’ of the ‘users_tel_nums’ table.
Now, when the structure and relations are described, let's see into the ‘search’ method of the ‘Objects’ model.
Let's announce a variable ‘ownerPhone’ in the ‘Objects’ model:
Add a name to the field:
- public function attributeLabels() {
- ...
- 'ownerPhone' => 'Owner phone number(s)',
- }
Let's save the entered value by search:
- public function rules() {
- ...
- array('ownerPhone', 'safe', 'on' => 'search'),
- }
Here is the ‘search()’ method:
- public function search() {
- $criteria = new CDbCriteria;
-
- $criteria->compare($this->getTableAlias().'.id', $this->id);
- $criteria->compare($this->getTableAlias().'.status', $this->status, true);
-
- if ($this->ownerPhone) {
- $criteria->compare('usersTelNums.number', $this->ownerPhone, true);
- }
-
- $criteria->order = $this->getTableAlias().'.id DESC';
- $criteria->with = array(
- 'usersTelNums' => array(
- 'select'=>'usersTelNums.number',
- 'together'=>true
- ),
- );
-
-
- return new CActiveDataProvider($this, array(
- 'criteria' => $criteria,
- 'pagination'=>array(
- 'pageSize'=>50,
- ),
- ));
- }
Please, pay attention on how you should specify ‘with’ for $criteria.
And how to set relation with the ‘UsersTelNums’ model:
- 'usersTelNums' => array(self::HAS_MANY, 'UsersTelNums', array('userId' => 'ownerId')),
CSRF and CMenu.
There is a ‘view.php’ view for data viewing of an object.
On the page there is a menu with links for editing and deleting the current object:
- $this->widget('zii.widgets.CMenu', array(
- 'items'=>array(
- array('label'=>tc('Update object'), 'url'=>'#', 'linkOptions'=>array('submit'=>array('update','id'=>$model->id))),
- array('label'=>tc('Delete object'), 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure want to remove the chosen element?')),
- ),
- ));
In a period of time you have decided to add a protection from CSRF (http://www.yiiframework.com/doc/guide/1.1/en/topics.security#cross-site-request-forgery-prevention)
And after clicking on such links an error appears: Error 400 "The CSRF token could not be verified".
The solution for the problem is to add 'csrf' => true.
So we will get as a result the following:
- $this->widget('zii.widgets.CMenu', array(
- 'items'=>array(
- array('label'=>tc('Update object'), 'url'=>'#', 'linkOptions'=>array('submit'=>array('update','id'=>$model->id), 'csrf' => true)),
- array('label'=>tc('Delete object'), 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure want to remove the chosen element?', 'csrf' => true)),
- ),
- ));