On cake 2.1, I need to SUM a field from the contained records.
Currently I get the sum of All records on the child table, but need to group by the id of my main table (employee).
Attend belongs to Employee
$agents = $this->Employee->find('all', array(
'fields' => array('Employee.FullNameNoId'),
'conditions' => $conditions,
'contain' => array(
'Attend' => array(
'conditions' => array(
'Attend.as_date BETWEEN ? AND ?' => array(
date('Y-m-d', strtotime($this->passedArgs['date1'])),
date('Y-m-d', strtotime($this->passedArgs['date2']))
)
),
'fields' => array('sum(Attend.as_labormin) AS total'),
//'group' => array('Attend.employee_id') // This gets sql errors below
)
)
));
Tried several combinations with SQL errors:
'group' => array('Attend.employee.id') // Model "Attend" is not associated with model "Attend"
//Column not found: 1054 Unknown column
'group' => array('employee_id') // Model "Attend" is not associated with model "employee_id"
// Column not found: 1054 Unknown column 'Attend.group' in 'field list'
'group' => array('Employee.id') //Column not found: 1054 Unknown column Attend.group' in 'field list'
Relation betweent tables is fine, I can get related records, problem is to get a sum by employee id.
Checked Cakephp SUM of related field, but it seems cumbersome to use SELECT SUM, and they left out the grouping needed.
Can you help?
Try disabling 'autoFields'. It is known to cause SQL errors with aggregate functions and 'group by' statements. Find out more here
Related
I am using cakephp 2.5, and need to join Vehicle table with Address table where vei_id is the foreign key
I have one find operation that is generating a wrong condition for the two models: Vehicle and Address.
Address has the vei_id column wich is the foreign key to join the vehicle table.
The query is generating vehicle_id as the column to join the two tables, the probem is that this column does not even exists.
I have mapped the two models using the vei_id column.
How can i avoid this situation ? seems cakephp try to guess the join column even if i have already write the condition using the column i want.
//Vehicle Model
public $hasOne = array(
'Address' => array(
'className' => 'Address',
'conditions' => array('Vehicle.vei_id = Address.vei_id'),
'foreignkey' => false
)
//Address Model
public $belongsTo = array(
'Vehicle' => array(
'className' => 'Vehicle',
'conditions'=> array('Vehicle.vei_id=Address.vei_id'),
'foreignKey' => 'vei_id'
),
);
//At vehiclecontroller
$data = $this->Vehicle->find('first', array(
'conditions' => array('Vehicle.vei_id' => $vehicleId),
'contain' => array(
'Address' => array('conditions'=> array('Address.vei_id'=>'Vehicle.vei_id',
'Vehicle.vei_id' => $vehicleId
),
)),
));
it generates this line :
LEFT JOIN Address ON (
Address.vehicle_id = Vehicle.vei_id
AND Address.vei_id = 'Vehicle.vei_id'
AND Vehicle.vei_id = 123
)
Where this column does not exists :
Address.vehicle_id = Vehicle.vei_id
Your query looks little bit confusing:
Just look at following conditions within contain:
'contain' => array(
'Address' => array('conditions'=>
array(
'Address.vei_id'=>'Vehicle.vei_id', // why is this ?
'Vehicle.vei_id' => $vehicleId
),
));
Why are you using following conditions within contain ?
Address.vei_id'=>'Vehicle.vei_id
Did you do that to join two tables ?
When you use contain these things are done by cakephp's convention.
Address table is already joined with vehicle table.
See here:Cakephp contain.
Also why not to follow cakephp convention?
If you have vehicles table,
the foreign key would be vehicle_id according to cakephp convention.
And if you have users table foreign key would be user_id.
These things also reduces your work and make things easier.
See Here: (cakephp model and database conventions).
I have a query like
$p = $this->Products->findById( $id )
->select(['name', 'description', 'category_id', 's.name', 'pp.price'])
->join([
'table' => 'sizes',
'alias' => 's',
'type' => 'INNER',
'conditions' => 's.category_id = Products.category_id',
])
->join([
'table' => 'products_prices',
'alias' => 'pp',
'type' => 'LEFT',
'conditions' => 'pp.size_id = s.id AND pp.product_id = Products.id',
]);
The problem is if the product has 10 different sizes, 10 rows will be produced with repeating name, description, category_id elements
Is there a way to rewrite it so the size's name and prices are delivered as array as a sub-array?
Maybe just use Containable instead of Join? You can run a find() on your Products and contain Sizes and ProductPrices. This would get you the array/sub-array you're looking for.
The only down-side is that it would still pull products even if they don't have a matching size.
It's tough to tell what you're really going for, since you're joining on category_id, which isn't really referenced. Maybe if you explain in more detail what the goal of this query is, we could provide a better answer on how to achieve.
In my PartsController I have the following code:
$serialNrs = $this->Part->find('list',
array(
'conditions'=>array(
'Part.status_id' => 3
),
'fields'=> array('serial_nr_id' )
//'fields'=> array('serialNr.name')
));
The according select field shows a list of id's instead of values.
When I use 'fields'=> array('serialNr.name') I get an error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'SerialNr.name' in 'field list'
SELECT Part.serial_nr_id, SerialNr.name FROM part145.parts AS Part WHERE Part.status_id = 3
What do I need to do to show a list of values?
You should use the Model name instead of the actual table name in database, thus
$serialNrs = $this->Part->find('list',
array(
'conditions'=>array(
'Part.status_id' => 3
),
'fields'=> array('Part.name' )
));
According to the above code, you have a model named "Part" where it points to a database table where there are at least a "status_id" column and a "name" column.
I am joining multiple tables like the following.
$recommend_logs = $this->RecommendingProductLog->find('all', array(
'recursive' => 2,
'fields' => array('Product.ProductName', 'Product.Gender', 'Product.Price', 'RecommendingProductLog.preference', 'Brand.BrandName'),
'conditions' => array('RecommendingProductLog.user_id' => $user_id),
'contain' => array('Product', 'Product.Brand')
));
I am getting this query from log.
SQL Query: SELECT `Product`.`ProductName`, `Product`.`Gender`, `Product`.`Price`, `RecommendingProductLog`.`preference`, `Brand`.`BrandName`, `Product`.`id` FROM `database`.`recommending_Product_log` AS `RecommendingProductLog` LEFT JOIN `database`.`Products` AS `Product` ON (`RecommendingProductLog`.`Product_id` = `Product`.`id`) WHERE `RecommendingProductLog`.`user_id` = 32
Even though 'Product' table is a child table of 'Brand' table, somehow I don't see 'Brand' table in the query. That's why I am getting an error 'Unknown column 'Brand.BrandName' in 'field list'.
I specified 'Brand' in the Perfume model as 'belongsTo' and 'Perfume' in RecommendingPerfumeLog model and 'Perfume' as hasMany in Brand model.
can somebody point where the problem is?
thanks.
Have you correctly put the foreign keys on the respective tables of the relation? In this case Product have to have a a key with the name brand_id and it must be a foreign key to the brand id field.
Also check this and try to put your joins.
I don't remenber this 'contain' field from the docs. Are you using the latest version?
I am using CakePHP Find query to get SUM() from two diff fields in two diff db tables:
$total = $this->Model->find('all',array('fields' =>
array(
'SUM(Model1.amount * Model2.purchase * Model3.rental * Model4.manu) as total'),
'group' => array( 'Model.trackby_id' ) )
);
I have found sum() method useful for same tables two diff fields, but it does not seems working when two diff fields from diff tables.
Anybody have a solution for this?
You need to specify your SQL fragment in the fields array in the parameters of the find method.
$total = $this->Model->find('all', array(
'fields' => array(
'SUM(Model.price + OtherModel.price) AS total'
),
'group' => 'Model.id'
));