CakePHP 1.3 Return all fields but with one of the fields as DISTINCT - cakephp

What command would I use to to do a 'find' which would return all fields with the proviso that one of the fields is DISTINCT, without listing all the fields in the 'fields' array?
One can do this:
$this->Car->find('all', array('fields' => array(DISTINCT Car.colour)));
but that just returns the 'colour' field.
I want to do something like:
$this->Car->find('all', array('fields' => array('*', DISTINCT Car.colour)));
The only way to return all the fields is to list them all in the 'fields' array but I want to avoid this.

$this->Car->find('first', array('conditions'=>array('Car.colour'=>'blue')));
This will return the result that you trying to get. I'm afraid this is not what you expected :)

Related

CakePHP find fields for related model

I have two models, Zones and Regions. I cannot figure out why Cake's find will not allow me to specify fields from the related tables.
These conditions, only requesting fields from the first table work fine.
$conditions = array('fields' => array('Zone.id','Zone.title','Zone.color'));
These conditions cause problems because... reasons?
$conditions = array('fields' =>
array('Zone.id','Zone.title','Zone.color','Region.id','Region.title'));
Basic query, this is how I'm using the conditions.
$result = $this->Zone->find('all',$conditions);
The SQL Cake is generating:
SELECT "Zone"."id" AS "Zone__id", "Zone"."title" AS "Zone__title", "Zone"."color"
AS "Zone__color", "Region"."id" AS "Region__id", "Region"."title" AS "Region__title"
FROM "public"."zones" AS "Zone" WHERE 1 = 1
Why is Cake assuming that the Region.fields are part of the Zones model? Why isn't this working as expected?
When you're doing this, you need to include both models in the find. Here is the working example I settled on:
$conditions = array('fields' =>
array('Zone.id','Zone.title','Zone.color','Region.id','Region.title'));
$this->Zone->Region->find('all',$conditions);

CakePHP Recursive Find Method

In my CakePHP app, I have the following model association:
Offer -> Order -> Coupon
Then, at OffersController.php, i need to write a find method to output
the number of coupons sold on each offer, assuming that some orders can have multiple coupons.
The problem is that I'm in the OffersController, so when I try to use find('count'), I just get the number of offer entries. I want to count the number of coupon entries.
I tried to do something like $this->Offer->Order->Coupon->find('count'), but it does not work.
I tried to use the ContainableHelper as well, but got into the same problem of counting the wrong entries.
How can I proceed?
You need the resulting SQL query to do some magic with COUNT and GROUP BY. Try to add a field like this (untested):
$this->Offer->Order->Coupon->find(
'all',
array(
// Generate a field that counts the coupons per Order
'fields' => array(
'COUNT(Coupon.id) AS coupon_count'
),
// Only look at Orders for the given Offer
'conditions' => array(
'Order.offer_id' => $id
),
// This is important: group by Order.
'group' => ('Coupon.order_id')
)
);
If your Order -> Coupon have a simple hasMany relationship, a more convenient approach would be to keep track of the existing Coupon count in every Order record using an extra coupon_count field and the counterCache function. CakePHP can automatically update that counter field for you.

CakePHP save multiple rows

I am using two models 'User' and 'UserImage'.. Where in i am going to save multiple rows into user_images table.. The view for UserImage is below..
echo $this->Form->input('UserImage.0.photo');
echo $this->Form->input('UserImage.1.photo');
echo $this->Form->input('user_id');
echo $this->Form->end(__('Submit'));
Then how to save multiple rows..
You should specify the model of each field if you have more models involved.
Also, you need to specify for each record the related field (user_id).
Try this:
echo $this->Form->input('UserImage.0.user_id');
echo $this->Form->input('UserImage.1.user_id');
Plus use this to save multiple records and multiple models:
$this->UserImage->saveAll($this->request->data);
See document of cakephp there is a method say saveAll()
Cake PHP Saving Your Data
From the doc of cakephp 2.x:
For saving multiple records of single model, $data needs to be a numerically indexed array of records like this:
$data = array(
array('title' => 'title 1'),
array('title' => 'title 2'),
);
$this->saveMany($data);
Note that we are passing numerical indexes instead of usual $data containing the Article key. When saving multiple records of same model the records arrays should be just numerically indexed without the model key.
It is also acceptable to have the data in the following format:
$data = array(
array('Article' => array('title' => 'title 1')),
array('Article' => array('title' => 'title 2')),
);
$this->saveMany($data);
I have tried the first one and it worked great.
HTH.
you could use this one saveMany() to insert each image as new record. I used same to solve my problem. Every thing looks alright in paul.ago 's answer but just change saveAll() to saveMany()
$this->UserImage->saveMany($this->request->data);

CakePHP: Containable behaviour how to restrict the same tables field

I've a table with a more then 15 fields and having several relations.
After using containable behavior I've full control over the other related table. But I do not want all the fields of main table.
How can I restrict the fields of the main table by using containable behavior.
Thanks in advance!!
#Vins
That would be:
$this->Model->find('all', array(
'fields' => array('field', 'field'),
'contain' => array(
'relatedModel',
'relatedModel'
)
));
You can pass order, limit and condition clauses as well this way.

In cakePHP's find(), how do I include a constant?

I'm looking to build a SQL query something like:
"SELECT email.member_id, 1 FROM email ... "
I'm using
$this->Email->find('list', array(
'fields' => array('Email.member_id', '1'), ...
CakePHP is generating:
SELECT `Email`.`member_id`, `Email`.`1` FROM `emails` AS `Email` ...
How do I specify that the 1 is a constant and not a database field?
Why I want to do this
I basically want to return an associative array with keys of member_id and values of 1. Would it be better to just get a straight array of member_ids and then transform that into my wanted data structure? I thought this would be faster.
Cake does not easily allow that. In your specific case I would be wondering why you want to have a constant field anyways. But in general do to stuff like that you are supposed to use Virtual Fields:
http://book.cakephp.org/view/1608/Virtual-fields
Basically you would modify model Email by adding this:
var $virtualFields = array(
'one' => '1'
);
Now you can do your query like this:
$this->Email->find('list', array(
'fields' => array('Email.member_id', 'Email.one'), ...
For the usecase you added you should just get a straight array of ids and fill it afterwards. Not only will be that faster, but also less hacky => easier to understand. You can do that easily with ´array_fill_keys()´.

Resources