I have a question which is driving me crazy and I have to admit I am not that experienced in CakePHP. As mentioned in this question,
Using DISTINCT in a CakePHP find function, using DISTINCT this way:
$this->Model->find('all', array('fields'=>'DISTINCT field_name'));
does not return DISTINCT values, instead it returns all rows. In fact, the DISTINCT here is completely pointless because, for some reason , CakePHP adds TableName.idin the SQL query (why?? can I remove the id reference??), effectively returning every DISTINCT primary key (=all rows=unhelpful).
So, I still want to return the DISTINCT values of a particular field_name column. Can I not do it using just the find('all') or find('list') function? Is it really that the proper way of doing it using this Set::extract() function described in the link above? That appears to be a overly indirect solution by CakePHP, normally Cake make my life easier. :-) What is the proper way of using find and DISTINCT together? Maybe DISTINCT doesn't work for find()?
Looking at the CookBook, they say: "A quick example of doing a DISTINCT query. You can use other operators, such as MIN(), MAX(), etc., in a similar fashion:"
<?php
array(
'fields' => array('DISTINCT (User.name) AS my_column_name'),
'order' = >array('User.id DESC')
)
?>
Source: http://book.cakephp.org/2.0/en/models/retrieving-your-data.html
This indicates that DISTINCT should be possible to use, but what is what here? Does (User.name) correspond to the field_name I want DISTINCT for or is my_column_name my field_name?
Finally, has any of this changed when migrating from CakePHP 1.x to CakePHP 2.x? Ie are the answers for CakePHP 1.x seen on Stackoverflow still relevant?
Thanks in advance!
Yes, the second snippet is the correct way to do a SELECT DISTINCT in CakePHP 2.x. User.name corresponds to the field name, in this case to the field name in the users table. my_column_name is an (optional) alias for the field name in the result set, i.e. instead of name the field will be named my_column_name in the result set.
The correct way of using distinct in find with condition is:
$this->Model->find('all', array('fields' => array('DISTINCT Model.database_fieldname'),'conditions' => array('Model.database_fieldname' =>$val )));
where $val contains some value which you want to pass in the query.
Have a Nice day
For CakePHP 3.X
To select distinct fields, you can use the distinct() method:
// Results in SELECT DISTINCT country FROM a table...
$query = $articles->find();
$query->select(['country'])
->distinct(['country']);
CookBook Documentation
Related
Due to having to having to import data from an old non cake app and oddly built database table I need to pass paginate an array of records it is allowed to display - is this possible?
Normally I would reorganise the data into proper relationships etc but due to time scales etc this is not possible.
To give you more info - in my users table I have a field that contains a list of ID's that relate to documents they are allowed to view. The field will contain something like
123,23,45,56,765,122,11.9,71,25
Each ID refers to a document the documents model. I know that normally you would create proper ACOs and AROs and let the ACL/Auth componant handle which users can access what but this isnt an option this time around. So I thought if I could do it via paginate/find it might be an option?
Any help would be really appreciated.
Thanks in advance
You wouldn't pass it a list of IDs, you'd use the IDs in your paginate conditions - something like below.
(Code written off top of my head, so pardon any syntax errors...etc. It should give you the right idea/path at least):
//Controller
$this->loadModel('User');
$this->loadModel('Document');
$user = $this->User->findById($userId);
$documentIds = explode(',', $user['User']['doc_ids']);
$this->paginate = array(
'conditions' => array(
'id' => $documentIds
)
));
$documents = $this->paginate('Document');
When you pass an array as a condtion (eg. 'id'=>$arrayOfIds), it uses MySQLs "IN" - something like:
... WHERE id IN (45, 92, 173)
What's the best way of doing this in cakephp:
I have a Posts model+view+controller with a "Post type" field in my database - linking to my post type id.
The post type ids are:
- Preview
- Review
- News
What I'm asking is: What's the best way (natively) of retrieving the post type name, without creating a table Post_types and linking it with a post_id.
I tried creating an array in the config lists, it worked but I think it can be better then this.
You could use the afterFind method of your Post model and add there a post_type field to your results. See also the cookbook.
From what I understand your post_type is a enum field with values preview, review, news or a varchar in which you know only add these 3 types, right?
You could try the following query
SELECT DISTINCT(post_type) FROM posts;
This will return all post_types that are being used in your posts table.
In Cake you could do this either with the find or query method.
# Using find
$this->Post->find('all', array(
'fields' => array('DISTINCT(post_type)')
);
# Or using the query directly (maybe easier in this case)
$this->Post->query("SELECT DISTINCT(post_type) FROM posts;");
associate your table posts with the table post_types with $hasOne.
With a e.g.
$this->Post->find('all');
you would get the post and the post_type in one array.
We have the Ebooks HABTM Tags. And we try to select the Ebooks that have belong to the tag with id=160.
Both use the containable behavior, so in the Ebooks Controller I have written the following:
$this->Ebook->contain('Tag.id = "160"');
$ebooks = $this->Ebook->find('all');
According the book this should return the needed result. Instead of that, a list of all ebooks is given back.
Note also that two queries are run, the first returns the list of all Ebooks and the second the ebooks that should be returned. Does anyone have any idea?
Thanks in advance
Yes, this query says "Find all Ebooks and include with them all Tags with the id 160".
contain does not limit the primary result, only the related results.
You need to make an SQL JOIN to the HATBM table and filter your primary results on it, like so:
$this->Ebook->bindModel(array('hasOne' => array('EbooksTag')));
$this->Ebook->find('all', array(
'conditions' => array('EbooksTag.tag_id' => 160)
));
I'm working on an app with extensive database relationships and I need to return a specific data object. I'm having trouble with it. Here's what I need it to look like:
AccommodationGroup =>
array(
['name']=>'Group1',
['AccommodationRoom']=> array(
[0]=> array(
['id']=>1,
['RoomIdentifier']=>'Cabin001',
),
[1]=> array(
['id']=>2,
['RoomIdentifier']=>'Cabin002'
)
)
)
AccommodationRoom is related to camp by camp_id. AccommodationGroup is related to AccommodationRoom by accommodation_group_id.
As you can see in the data object example, I need to retrieve all the groups for a particular camp with the rooms of each group as a nested array of the group, and all this restricted to a particular camp.
I've tried to get at it by selecting all the applicable groups using findAllByCampId() which, of course, doesn't work (knew it wouldn't but tried it anyway). Another table, AccommodationRoomsCamp is a transitional table between AccommodationRooms and Camps. One way to do it would be:
$this->AccommodationRoomsCamp->findAllByCampId($id, array('group'=>'AccommodationRoomsCamp.accommodation_group_id'))
but accommodation_group_id is not stored in AccommodationRoomsCamp because it is already being stored in the AccommodationRoom table. I think that I need to do more than one operation, but I'm baffled. Little bit of a newb. Ideas?
Have a little bit of a hard time following how your tables are related - but I think you can solve your problem using recursive finds. Something like this in your controller:
$this->AccomodationRoomsCamp->recursive = 2;
$this->AccomodationRoomsCamp->find('all, array('conditions' => array(bla bla bla bla
See http://book.cakephp.org/view/1063/recursive
I have a n...n structure for two tables, makes and models. So far no problem.
In a third table (products) like:
id
make_id
model_id
...
My problem is creating a view for products of one specifi make inside my ProductsController containing just that's make models:
I thought this could work:
var $uses = array('Make', 'Model');
$this->Make->id = 5; // My Make
$this->Make->find(); // Returns only the make I want with it's Models (HABTM)
$this->Model->find('list'); // Returns ALL models
$this->Make->Model->find('list'); // Returns ALL models
So, If I want to use the list to pass to my view to create radio buttons I will have to do a foreach() in my make array to find all models titles and create a new array and send to the view via $this->set().
$makeArray = $this->Make->find();
foreach ($makeArray['Model'] as $model) {
$modelList[] = $model['title'];
}
$this->set('models', $models)
Is there any easier way to get that list without stressing the make Array. It will be a commom task to develops such scenarios in my application(s).
Thanks in advance for any hint!
Here's my hint: Try getting your query written in regular SQL before trying to reconstruct using the Cake library. In essence you're doing a lot of extra work that the DB can do for you.
Your approach (just for show - not good SQL):
SELECT * FROM makes, models, products WHERE make_id = 5
You're not taking into consideration the relationships (unless Cake auto-magically understands the relationships of the tables)
You're probably looking for something that joins these things together:
SELECT models.title FROM models
INNER JOIN products
ON products.model_id = models.model_id
AND products.make_id = 5
Hopefully this is a nudge in the right direction?
Judging from your comment, what you're asking for is how to get results from a certain model, where the condition is in a HABTM related model. I.e. something you'd usually do with a JOIN statement in raw SQL.
Currently that's one of the few weak points of Cake. There are different strategies to deal with that.
Have the related model B return all ids of possible candidates for Model A, then do a second query on Model A. I.e.:
$this->ModelB->find('first', array('conditions' => array('field' => $condition)));
array(
['ModelB'] => array( ... ),
['ModelA'] => array(
[0] => array(
'id' => 1
)
)
Now you have an array of all ids of ModelA that belong to ModelB that matches your conditions, which you can easily extract using Set::extract(). Basically the equivalent of SELECT model_a.id FROM model_b JOIN model_a WHERE model_b.field = xxx. Next you look for ModelA:
$this->ModelA->find('all', array('conditions' => array('id' => $model_a_ids)));
That will produce SELECT model_a.* FROM model_a WHERE id IN (1, 2, 3), which is a roundabout way of doing the JOIN statement. If you need conditions on more than one related model, repeat until you have all the ids for ModelA, SQL will use the intersection of all ids (WHERE id IN (1, 2, 3) AND id IN (3, 4, 5)).
If you only need one condition on ModelB but want to retrieve ModelA, just search for ModelB. Cake will automatically retrieve related ModelAs for you (see above). You might need to Set::extract() them again, but that might already be sufficient.
You can use the above method and combine it with the Containable behaviour to get more control over the results.
If all else fails or the above methods simply produce too much overhead, you can still write your own raw SQL with $this->Model->query(). If you stick to the Cake SQL standards (naming tables correctly with FROM model_as AS ModelA) Cake will still post-process your results correctly.
Hope this sends you in the right direction.
All your different Make->find() and Model->find() calls are completely independent of each other. Even Make->Model->find() is the same as Model->find(), Cake does not in any way remember or take into account what you have already found in other models. What you're looking for is something like:
$this->Product->find('all', array('conditions' => array('make_id' => 5)));
Check out the Set::extract() method for getting a list of model titles from the results of $this->Make->find()
The solution can be achieved with the use of the with operation in habtm array on the model.
Using with you can define the "middle" table like:
$habtm = " ...
'with' => 'MakeModel',
... ";
And internally, in the Model or Controller, you can issue conditions to the find method.
See: http://www.cricava.com/blogs/index.php?blog=6&title=modelizing_habtm_join_tables_in_cakephp_&more=1&c=1&tb=1&pb=1