I have a service where users are able to upload cars to the site, and select the manufacturer and model from a list. If the car model does not exist, the user can save a new model under the specific manufacturer.
Now to my problem, all models have to be confirmed by a moderator as being a valid type, having a correct spelling, and not being a duplicate etc. All confirmed models are visible to other users. I do this by specifying a condition in the Car's association as below:
$belongsTo = array(
'CarModel' => array(
'className' => 'CarModel',
'foreignKey' => 'car_model_id',
'conditions' => array('CarModel.confirmed' => 1),
'fields' => '',
'order' => ''
));
However, I want to override this condition in the edit action so that the user can see his models, even if they are not confirmed yet. Is that possible in the find conditions?
Regards,
Roland.
You can bind model before calling find() method. Try this in your action.
$this->ModelName->bindModel(
array('belongsTo' => array(
'CarModel' => array(
'className' => 'CarModel',
'conditions' => array('CarModel.confirmed' => 0), // Define your new condition here
)
)
)
);
$data = $this->ModelName->find('all'); // Now you will get the data for new condition applied
Hope it will work.
Related
I`m having a problem with the Containable behaviour.
I would like to know if there is any way to access the contained model attributes for operations like ordering.
For example, I have a model B which belongs to a Model A. I need to order objects of B using an attribute (integer) of A. It would be something like:
'contain' => array(
'A' => array(
'B' => array(
'order' => 'A.integer_attribute'
)
)
)
I know that there are easier ways to do this without Containable, but for reasons which are not worth being detailed here, I need to use it. This is an abstract example, in truth model A belongs to other models and this is just a small part of a deep containable tree.
I'd be very glad with any help!
EDIT
OK, I'll try my best to describe the situation without being unclear:
I have 4 models: User, Category, Field and UserField, and their relationships are as follows:
Category hasMany User
Category hasMany Field
User hasMany UserField
Field hasMany UserField
The opposite of these relations are all belongsTo. The purpose here is that the user belongs to a category, which has many fields that he needs to fill with his information (city, state etc). The information he fills is stored in the UserField table, as each information needs to have its field and the user who provided it.
That said, I need to build a screen which displays, for each category, a list of users and their information. So, I retrieve all the categories and its fields, so I can build a table for each category. Each field has an attribute "no_order", which is the number that indicates the order in which the field appears.
At the same time, I need all of each category's users to display them correctly in the tables. Finally, and there's the problem, I need to have UserField objects ordered by the "no_order" of their respective fields, for each user. So I ended up with something like:
$categories = $this->Category->find('all', array(
'order' => 'Category.name',
'contain' => array(
'Field',
'User' => array(
'UserField' => array(
'order' => 'Field.no_order'
)
)
)
));
But this doesn't work, since UserField cannot reach its respective Field's no_order from there.
I apologize if this wasn't clear enough, but for anyone who would spend a little while reading this, I would be VERY grateful for your help!
I am not shure for what do you want so, but I thing that should use joins of cakephp
$A = $this->A->find('all', array(
'joins' => array(
array(
'table' => 'B',
'alias' => 'B',
'type' => 'LEFT',
'conditions' => array(
'B.field_id = A.id'
)
)
),
'conditions' => array(
'B.field' => 'if_you_want_more_conditions'
),
'contain' => array(
'A' => array(
'B' => array(
'order' => 'A.integer_attribute'
)
)
),
'fields' => array('A.field','B.field'),
'recursive' => -1
));
I finally came up with a solution. I don't know how efficient it is, but there it goes:
'contain' => array(
'Field',
'User' => array(
'User.privilege = "Solicitante"'
'UserField' => array(
'order' => '(SELECT f.no_order FROM fields AS f WHERE UserField.field_id = f.id LIMIT 1)'
)
)
)
Having the raw query solved my problem. Hope it helps anybody who comes across a similar problem!
I have a function where a user can request a project. The request has 2 fields for other employees to be added.
It's got a field for a person who is responsible for the project (person_responsible) and the employee who is supposed to attend the opening meeting (person_attending).
What I want to know is, since both these fields (person_responsible and person_attending) will be pulling it's data from hr_employees table, how would I set this up in my Project-Model.
At the moment I have the one field set up like this:
public $belongsTo = array(
'HrEmployee' => array(
'className' => 'HrEmployee',
'foreignKey' => 'responsible_person',
'fields' => 'HrEmployee.employeename',
)
);
How would I set up the other field?
What I do in this cases is to make two associations. Since cake allow to customize relations, you can have two relations to the same model with different names.
public $belongsTo = array(
'ResponsibleEmployee' => array(
'className' => 'HrEmployee',
'foreignKey' => 'responsible_person',
'fields' => 'HrEmployee.employeename',
),
'AttendingEmployee' => array(
'className' => 'HrEmployee',
'foreignKey' => 'person_attending',
'fields' => 'HrEmployee.employeename',
)
);
Change the names to adjust your needs. Now, if your model is set as containable and you retrieve the Project model with those models in it, you'll get something like
array('Project' => array(/*data project*/),
'ResponsibleEmployee' => array(/*name*/),
'AttendingEmployee' => array(/*name*/)
)
(or another variation of that array depending on how the query was made).
I have an issue trying to retrieve associated model data from another associated model.
I have Donor Model which is associated with BloodGroup Model (belongsTo/hasMany rel).
Now, I also have a Donation Model which is associated with Donor Model (again, belongsTo/hasMany rel).
From the Donation Model, I want to retrieve data from the BloodGroup Model.
Currently only the Donor related data is retrieved. The idea is that I do not add a blood group field inside the donation, but map the blood group of a donation, on the fly, through the donor that made the donation!
I have been checking out the containable Behavior but I am not sure if this is how I can do it! It seems that it is used to 'contain' and filter data from associated models rather than to extend associations.
Any help appreciated very much, as always!
[EDIT]
DONOR MODEL
class Donor extends AppModel{
public $belongsTo = array(
'BloodGroup'=> array(
'className' => 'BloodGroup'
),
'DonorType' => array(
'className' => 'DonorType'
)
);
public $hasMany = array(
'Donation' => array(
'className' => 'Donation',
'foreignKey' => 'donor_id',
'order' => 'Donation.created DESC',
'limit' => 10,
'dependent' => true
)
);
DONATION MODEL
public $belongsTo = array(
'Donor' => array(
'className' => 'Donor',
'counterCache' => true,
)
);
From the Donation Controller :
public $paginate = array(
'order' => array("Donation.d_date" => 'desc'),
'limit' => 10
);
$this->Paginator->settings = $this->paginate;
$donations = $this->Paginator->paginate('Donation');
$this->set('donations',$donations)
Result retrieved :
[donations] => Array
(
[0] => Array
(
[Donation] => Array
(
[id] => 1
[donor_id] => 4
etc..
)
[Donor] => Array
(
[id] => 4
[name] => ...
)
)
)
It's not clear what data you actually want to get, but to answer what appears to be your hope:
"From the Donation Model, I want to retrieve data from the BloodGroup
Model."
Because there's a related path, you can get BloodGroup data, from the Donation model like this:
$this->Donor->BloodGroup->find('all');
Or by using Containable:
$this->find('all', array(
'contain' => array(
'Donor' => array(
'BloodGroup'
)
)
));
There's a lot of options - it just depends on what data you need.
For details on how I use find()s in my Model while still paginating, see this answer: https://stackoverflow.com/a/6577042/673664
(there are likely a lot of ways to do this - this is just the one I use, and works great for me)
If the BloodGroup has many Donor and if Donor has many Donation, You can retrieve Donation data from BloodGroup but you have to set the recursive to 2 ($this->Donation->recursive = 2;) before you use find method. If you want output blood group data from donation. Try that.
I have hasMany Through table which is Chats table with Chat model and I'm using loadModel in User controller to load Chat model then ran below query to bindModel with Chat.user_id and User.id :
$this->loadModel('Chat');
$this->Chat->bindModel(array(
'belongsTo' => array(
'User' => array(
'foreignKey' => false,
'conditions' => array('Chat.user_id = User.id')
)
)
));
$lastChat = $this->Chat->find('all', array(
'conditions' => array(
'Chat.receiver_id' => $user_id['User']['id']
),
'order' => array('Chat.id DESC'),
'fields' => array(
'Chat.id',
'Chat.chat',
'Chat.user_id',
'Chat.receiver_id',
'Chat.read',
'Chat.created'
),
'group' => array('Chat.user_id')
));
I want to join those tables together but this does not seem to work in Cake way I tried with normal SQL query and it works fine.
What could be wrong here?
Have you tried setting the recursive property before the find? Eg:
$this->Chat->recursive = 3;
You may need to set this after $this->Chat->bindModel, but I am not sure if this will make a difference or not. You will also need to re-bind the User model before each find, if, for example, your find queries are run within a loop ...
Im using cakephp and Im stuck about the idea of listing multiple models, here's my scenario:
I have two major models namely Task and Events. I used the Events model to track all changes. this has the ff fields: id, model, model_id, changed
I used Events table to track multiple model changes. so when something changed to Task model it will be logged to Events models (get the idea??)
What I want to do is whenever i used the find method for Event model, i want to list the model information (for example, the Task model) together with my Events information like this:
array(
[0] => array (
[Event] => array(
id => 1
model => Task
model_id => 2
changed => array()
)
[Task] => array(
id => 2
name => Task 1
descp => Test Task
)
)
)
Note: The model can be any model, it can be Projects model.
At my task.php, it is no problem because i can easily declare:
var $hasMany = array(
'Events' => array(
'className' => 'Events',
'foreignKey' => 'model_id',
'dependent' => false,
'conditions' => array('Events.model' => 'Task')
)
);
I can get Events information using the find method on task although using
$this->Task->Event->find('all');
will not work, i dont know why.
So as soon as I declare, something like this in my Event Model:
var $belongsTo = array(
'Task' => array(
'className' => 'Task',
'foreignKey' => 'model_id',
'conditions' => array('Event.model' => 'Task', 'Event.model_id' => 'Task.id'),
'fields' => '',
'order' => ''
)
);
will throw an SQL error. Do you guys have an idea how to implement it??
Thanks in advanced. :)
I use polymorphic models all the time. The reason that $this->Task->Event->find('all') doesn't work is that you were performing the find on the Event model which, until you defined its belongsTo association, didn't have any means of retrieving the Task info.
As for now, the only thing I see that looks odd is that you've aliased your Task association as Events (plural) rather than its singular version which is the convention for models. You also identified the className as the plural version.