Waiting for Dev...
Waiting for Dev...

Reputation: 13029

Yii: Find in Active Record by a PHP Expression

Say you have an Active Record model which contains a set of records:

id | name
---------
1 | Record1
2 | Record2
3 | Record3

Users who has the permission to see each records are stored in another table, using a foreign key to represent the record, in a comma separated way:

foreignId | users
-----------------
1         | joe, doe, zoe
2         | joe
3         | doe, zoe

There is an authentication manager bizRule which checks if current user has the permission to see a record. You give it the record id and it checks the permissions table to see if the user is in the comma separeted field.

Yii::app()->authManager->checkAccess('seeRecord', $id);

Is there an easy way using CActiveRecord to pass a PHP Expression "query"? Something like:

Record::model()->findByPHPExpression('Yii::app()->authManager->checkAccess('seeRecord', array('id' => 'id'));

If the expression returns true for the current record, that record would be added.

Thank you

Upvotes: 0

Views: 707

Answers (1)

Asgaroth
Asgaroth

Reputation: 4334

You have some serious non-yii related issue, your database schema is wrong, please read some about database normalization.

You should have an intermediate table, if a user can see various records, and a record can be seen by various users, then you need an intermediate table.

Users, Users_cansee_Records, Records

The intremediate table will have 2 primary keys, that are the user_id and record_id respectively

for your example this table will have something like:

user | record
--------------
1 | joe
1 | doe
1 | zoe
2 | joe
3 | doe
3 | zoe

Yii supprots this "Many many" relationships out of the box. but please read about database normalization, its an important topic, database design is a critical step in any project development.

Upvotes: 4

Related Questions