Cleaner way to get HABTM value with associated Model using CakePHP - cakephp

I have the following setup:
Model: User
Model: Client
User hasAndBelongsToMany Client
Client hasAndBelongsToMany User
DB Tables:
Users
id
lots of fields
Clients
id
lots of fields
Cliens_Users
id
client_id
user_id
status
Everything is working just peachy but I am wondering if there is a better way to get the status field (associated with the HABTM table) with my Client model.
Here is what I'm doing:
ClientsController.php
public function view($id = null) {
$this->Client->id = $id;
if (!$this->Client->exists()) {
throw new NotFoundException(__('Invalid client'));
}
$this->Client->contain('User.id');
$this->Client = $this->Client->read(null, $id);
$this->set('client', $this->Client);
}
the var_dump on my $this->Client looks like this:
array
'Client' =>
array
'id' => string '1' (length=1)
'first_name' => string 'Matt' (length=4)
'last_name' => string 'Moses' (length=4)
'address_1' => string '123 S Happy Lane' (length=16)
'address_2' => string '' (length=0)
'city' => string 'Yourtown' (length=5)
'state' => string 'TX' (length=2)
'zip' => string '12345' (length=5)
'phone_home' => string '8014567899' (length=10)
'phone_work' => string '8014567899' (length=10)
'phone_cell' => string '8014567899' (length=10)
'email' => string 'matt#matt.com' (length=13)
'created' => string '2012-06-13 04:41:19' (length=19)
'modified' => string '2012-06-13 04:41:19' (length=19)
'User' =>
array
0 =>
array
'id' => string '1' (length=1)
'ClientsUser' =>
array
...
Clients/view.ctp
...
<?php echo $client['User'][0]['ClientsUser']['status']; ?>
...
To me this last line screams "there is a cleaner way" but I can't seem to find anything on the matter. Any thoughts?

Related

yii activerecord return object

I am using using yii2 to in basic template with MySQL database
Why This code returns an object instead of an array of selected records from Database
when i use var_damp($rooms) the output is seems an object and not an array of selected record in array format;
any body can help
public function actionIndexFiltered()
{
$query = Room::find();
$searchFilter = [
'floor' => ['operator' => '', 'value' => ''],
'room_number' => ['operator' => '', 'value' => ''],
'price_per_day' => ['operator' => '', 'value' => ''],
];
if(isset($_POST['SearchFilter']))
{
$fieldsList = ['floor', 'room_number', 'price_per_day'];
foreach($fieldsList as $field)
{
$fieldOperator = $_POST['SearchFilter'][$field]['operator'];
$fieldValue = $_POST['SearchFilter'][$field]['value'];
$searchFilter[$field] = ['operator' => $fieldOperator, 'value' => $fieldValue];
if( $fieldValue != '' )
{
$temp1=$query->andWhere([$fieldOperator, $field, $fieldValue]);
}
}
}
$room1=$temp1->all();
$rooms = $query;
return $this->render('indexFiltered', [ 'rooms' => $rooms, 'searchFilter' => $searchFilter,'room1'=>$room1 ]);
}
Output is like this and this shows that this code return a query object and not an array of query execution on database ,
so i checked the code using var_dump function the results is that it is returning a object not array
object(yii\db\ActiveQuery)[70]
public 'sql' => null
public 'on' => null
public 'joinWith' => null
public 'select' => null
public 'selectOption' => null
public 'distinct' => null
public 'from' =>
array (size=1)
0 => string 'room' (length=4)
public 'groupBy' => null
public 'join' => null
public 'having' => null
public 'union' => null
public 'params' =>
array (size=0)
empty
private '_events' (yii\base\Component) =>
array (size=0)
empty
private '_behaviors' (yii\base\Component) =>
array (size=0)
empty
public 'where' =>
array (size=3)
0 => string '=' (length=1)
1 => string 'room_number' (length=11)
2 => string '3' (length=1)
public 'limit' => null
public 'offset' => null
public 'orderBy' => null
public 'indexBy' => null
public 'emulateExecution' => boolean false
public 'modelClass' => string 'app\models\Room' (length=15)
public 'with' => null
public 'asArray' => null
public 'multiple' => null
public 'primaryModel' => null
public 'link' => null
public 'via' => null
public 'inverseOf' => null
This code return an a dataProvider .. alias the code for get models /active record
$query = Room::find();
for obtain the all the models you can use
$roomModels= Room::find()->all();
in this the result is a collection of obejct (models) of class Room
if you need an array you can use
$roomArray = Room::find()->asArray()->all();
In your code $temp1 is the same as $query. andWhere() method returns $this which means that
$temp1 = $query->andWhere([$fieldOperator, $field, $fieldValue]);
makes $temp1 to be the same as $query which is object of yii\db\ActiveQuery.
Now you are calling this:
$room1 = $temp1->all(); // the same as $room1 = $query->all();
and this holds array of Room objects while
$rooms = $query;
is just another unnecessary assignment because $rooms is the same as $query which is object of yii\db\ActiveQuery.
Finally i found the reason for this problem is that
I must call asArray() member function of ActiveRecord class to generate an array output
$room1=$temp1->all(); will changed to $room1=$temp1->asArray()->all();

Cakephp - SQL not being generated for all records being inserted

I have the following code in my controller to insert records.
//Perform Inserts
$success = true;
foreach($toinsertrecords as $toinsertrecord) {
$this->BillingCenterDetail->create();
if (!$this->BillingCenterDetail->save($toinsertrecord)) {
$success = false;
echo "insert fail";
} else {echo "insert success";}
}
if ($troubleshoot) {
$log = $this->BillingCenterDetail->getDataSource()->getLog(false, false);
echo "<pre>Output from Datasource Log";
var_dump($log);
echo "</pre>";
}
The array being looped at, has the following contents (Note that the last record has the field order slightly switched, but as this is an associative array, i thought it should not matter)
array (size=3)
0 =>
array (size=1)
'BillingCenterDetail' =>
array (size=12)
'startdate' => string '2014-03-10' (length=10)
'enddate' => string '2014-03-10' (length=10)
'billing_center_id' => string '50' (length=2)
'isactive' => boolean false
'addr1' => string 'melbourne' (length=9)
'addr2' => string 'melbourne' (length=9)
'addr3' => string 'melbourne' (length=9)
'city' => string 'melbourne' (length=9)
'postcode' => string '777' (length=3)
'country' => string 'aus' (length=3)
'email' => string 'a#a.com' (length=7)
'phone' => string '1234' (length=4)
1 =>
array (size=1)
'BillingCenterDetail' =>
array (size=12)
'startdate' => string '2014-03-12' (length=10)
'enddate' => string '2028-12-10' (length=10)
'billing_center_id' => string '50' (length=2)
'isactive' => boolean false
'addr1' => string 'melbourne' (length=9)
'addr2' => string 'melbourne' (length=9)
'addr3' => string 'melbourne' (length=9)
'city' => string 'melbourne' (length=9)
'postcode' => string '777' (length=3)
'country' => string 'aus' (length=3)
'email' => string 'a#a.com' (length=7)
'phone' => string '1234' (length=4)
2 =>
array (size=1)
'BillingCenterDetail' =>
array (size=12)
'billing_center_id' => string '50' (length=2)
'startdate' => string '2014-03-11' (length=10)
'enddate' => string '2014-03-11' (length=10)
'isactive' => string '1' (length=1)
'addr1' => string 'test' (length=4)
'addr2' => string 'test' (length=4)
'addr3' => string 'test' (length=4)
'city' => string 'test' (length=4)
'postcode' => string 'test' (length=4)
'country' => string 'test' (length=4)
'email' => string 'test#test' (length=9)
'phone' => string 'test' (length=4)
This is the output from my getDataSource()->getLog statement
Output from Datasource Log
array (size=3)
'log' =>
array (size=4)
0 =>
array (size=5)
'query' => string 'SELECT `BillingCenterDetail`.`id`, `BillingCenterDetail`.`startdate`, `BillingCenterDetail`.`enddate`, `BillingCenterDetail`.`billing_center_id`, `BillingCenterDetail`.`isactive`, `BillingCenterDetail`.`addr1`, `BillingCenterDetail`.`addr2`, `BillingCenterDetail`.`addr3`, `BillingCenterDetail`.`city`, `BillingCenterDetail`.`postcode`, `BillingCenterDetail`.`country`, `BillingCenterDetail`.`email`, `BillingCenterDetail`.`phone`, `BillingCenterDetail`.`created`, `BillingCenterDetail`.`modified` FROM `bm`.`bil'... (length=718)
'params' =>
array (size=0)
...
'affected' => int 1
'numRows' => int 1
'took' => float 0
1 =>
array (size=5)
'query' => string 'BEGIN' (length=5)
'params' =>
array (size=0)
...
'affected' => int 1
'numRows' => int 1
'took' => float 0
2 =>
array (size=5)
'query' => string 'INSERT INTO `bm`.`billing_center_details` (`startdate`, `enddate`, `billing_center_id`, `isactive`, `addr1`, `addr2`, `addr3`, `city`, `postcode`, `country`, `email`, `phone`, `modified`, `created`) VALUES ('2014-03-10', '2014-03-11', 50, '0', 'melbourne', 'melbourne', 'melbourne', 'melbourne', '777', 'aus', 'a#a.com', '1234', '2014-03-11 15:47:15', '2014-03-11 15:47:15')' (length=374)
'params' =>
array (size=0)
...
'affected' => int 1
'numRows' => int 1
'took' => float 0
3 =>
array (size=5)
'query' => string 'INSERT INTO `bm`.`billing_center_details` (`startdate`, `enddate`, `billing_center_id`, `isactive`, `addr1`, `addr2`, `addr3`, `city`, `postcode`, `country`, `email`, `phone`, `modified`, `created`) VALUES ('2014-03-13', '2028-12-10', 50, '0', 'melbourne', 'melbourne', 'melbourne', 'melbourne', '777', 'aus', 'a#a.com', '1234', '2014-03-11 15:47:15', '2014-03-11 15:47:15')' (length=374)
'params' =>
array (size=0)
...
'affected' => int 1
'numRows' => int 1
'took' => float 0
'count' => int 4
'time' => float 0
You can clearly see that it has only tried to insert the first 2 records, but for some reason it hasn't inserted the 3rd record. Based on the log, it also didn't generate the 3rd Insert SQL statement?
By the time it hits the 3rd loop, the "Save" function fails, this causes $success = false in the end.
Can anyone guess why this is happening?
My guess is the data in the third entry isn't passing validation.
Possibly your model has the postcode field as numeric only? In that case this data will fail:
'postcode' => string 'test' (length=4)
To debug this, I'd add this line inside your existing failure routine:
var_dump($this->BillingCenterDetail->invalidFields());

cakephp How can I convert passed params to named params to prefill element form?

I'm using CakePHP version 2.2.3 I have an element with a search box and a few dropdowns that use CakeDC's search plugin. It works great and just passes the selected/searched items in the URL like this www.mydomain.com/products/cid:1/mid:3/terms:these%20terms where cid is category id, and mid is manufacturer id.
I created pages that allow you to click a category to find all products in that category, but I can't get the category select box, in the element, to select the category of the page it is on. It works if I use the same URL structure as my element submits but I want a clean URL for SEO so I setup the following custom route:
/**
* Categories
*/
Router::connect(
'/products/category/:cid-:slug', // E.g. /products/category/3-my_category
array('controller' => 'products', 'action' => 'category'),
array(
'pass' => array('cid', 'slug'),
'cid' => '[0-9]+'
)
);
this results in a nice looking URL but doesn't pre-select the value of my select list.
I was able to get it working with the code below in my element, but it seems "hacky/clunky"
if(isset($this->params['named']['cid']) && !empty($this->params['named']['cid'])){
echo $this->Form->input('cid', array('label' => false, 'default' => $this->params['named']['cid'], 'options' => $categories, 'empty' => ' ( Category ) '));
}elseif(isset($this->params['pass']['0']) && !empty($this->params['pass']['0'])){
echo $this->Form->input('cid', array('label' => false, 'default' => $this->params['pass']['0'], 'options' => $categories, 'empty' => ' ( Category ) '));
}else{
echo $this->Form->input('cid', array('label' => false, 'options' => $categories, 'empty' => ' ( Category ) '));
}
Also, in my controller I've tried this:
$this->params['named']['cid'] = $this->params['pass']['0'];
but I get this error: Indirect modification of overloaded element of CakeRequest has no effect
I believe the plugin automatically sets the selected value if using named params, unless thats a default behavior of cake. How can I convert the passed params to named params, or can I force my plugin to use passed params?
output from var_dump($this->$params):
object(CakeRequest)[9]
public 'params' =>
array
'plugin' => null
'controller' => string 'products' (length=8)
'action' => string 'category' (length=8)
'named' =>
array
empty
'pass' =>
array
0 => string '2' (length=1)
1 => string 'This_and_that' (length=13)
'cid' => string '2' (length=1)
'slug' => string 'This_and_that' (length=13)
public 'data' =>
array
empty
public 'query' =>
array
empty
Thanks

cakephp saveAll associated model field is not being saved

I'm currently using cakephp 2.2.3.
I have the following Model Associations:
VehicleModel -> Vehicle -> Order
Plan -> Order
Vehicle HABTM Tag
Inside the Vehicle controller, add action, I have:
if(!empty($this->request->data)) {
if($this->Vehicle->saveAll($this->request->data)) {
$this->Session->setFlash('Vehicle was successfully added.');
}
}
The $this->request->data array is formatted like this:
array(
'VehicleModel' => array(
'category_id' => '2',
'make_id' => '1'
),
'Order' => array(
'plan_id' => '2'
),
'Vehicle' => array(
'vehicle_model_id' => '13',
'price' => ' 8700',
'year' => '1994',
'km' => '100',
'color' => '61',
'fuel' => '1',
'gear' => '20',
'type' => '51',
'city' => 'Rio de Janeiro',
'state' => 'RJ'
),
'Tag' => array(
'Tag' => array(
(int) 0 => '69',
(int) 1 => '11'
)
)
)
The orders table has the following fields:
id , plan_id , vehicle_id , created , modified.
Vehicle Model:
class Vehicle extends AppModel {
public $belongsTo = array('User' , 'VehicleModel');
public $hasMany = array('Order' , 'Image');
public $hasAndBelongsToMany = array('Accessory' , 'Tag');
}
Order Model:
class Order extends AppModel {
public $belongsTo = array('Vehicle' , 'Part' , 'Plan');
public $validate = array(
'plan_id' => array(
'rule' => 'notEmpty'
)
);
}
The problem I'm having is that the Order.plan_id field is not being saved, although all other fields are being saved normally. What can I be doing wrong?
Just to be clear, When I do the multiple saving manually, everything
works just fine. I mean, when I write:
$this->Vehicle->save()
and then set
$this->request->data['Order']['vehicle_id'] = $this->Vehicle->id
and finally
$this->Vehicle->Order->save()
everything works just fine. It's the saveAll that is causing me
trouble.
If that is the case, see where $this->request->data['Order']['vehicle_id'] = $this->Vehicle->id. Comparing this to your var dump above, order never contains the relation to the main model, which leads me to ask if this is a new record you are trying to save or an update? I think you might have to not go with a saveAll here if you are setting a new record because the main id is not yet set. Please see:
http://book.cakephp.org/2.0/en/models/saving-your-data.html#saving-related-model-data-hasone-hasmany-belongsto
Particularly: "If neither of the associated model records exists in the system yet (for example, you want to save a new User and their related Profile records at the same time), you’ll need to first save the primary, or parent model."
They basically do the long version you are doing.

Retireving data from arrays

I have this data spited out from my cake php query.. This is the problem.. How can i retrieve this data so that it fits this structure. How can I do this in my view?, I am just having trouble understanding how to retrieve each value
so this part I want it in a <p></p> in my page
array
'id' => string '1' (length=1)
'category_name' => string 'Appetizers' (length=10)
'category_keywords' => string 'appetizer, appetizers' (length=21)
'category_title' => string 'Our Side Dishes' (length=15)
'category_info' => string 'Hello This is the category info' (length=30)
'dish_id' => string '1' (length=1)
This other part returned of the query i want to put it in a 3 x 2 table. like
<table>
<tr>
<td> the data from the array 0</td>
<td> the data from the array 1</td>
<td> the data from the array 2>/td>
</tr>
<tr>
<td> the data from the array 3</td>
<td> the data from the array 4</td>
<td> the data from the array 5>/td>
</tr>
here are all the those arrays. I would like to use a for loop with a mod if 3 %== 0 and then create a new <tr>, i tried it but it gave me an error...
array
'id' => string '1' (length=1)
'category_name' => string 'Appetizers' (length=10)
'category_keywords' => string 'appetizer, appetizers' (length=21)
'category_title' => string 'Our Side Dishes' (length=15)
'category_info' => string 'Hello This is the category info' (length=441)
'dish_id' => string '1' (length=1)
array
0 =>
array
'id' => string '1' (length=1)
'dish_name' => string 'Arepa Con Pollo!' (length=16)
'dish_disc' => string 'Exquisite corn patti, the best yyy' (length=34)
'dish_price' => string '2.90' (length=4)
'dish_image' => string '/img/arepa rellena con pollo.jpeg' (length=33)
'dish_category_id' => string '1' (length=1)
'dish_price_label' => string 'Delicious Arepa ' (length=16)
1 =>
array
'id' => string '2' (length=1)
'dish_name' => string 'Arepa Con Queso' (length=15)
'dish_disc' => string 'Corn patie with Cheese' (length=22)
'dish_price' => string '2.65' (length=4)
'dish_image' => string '/img/arepa rellena con queso.jpeg' (length=33)
'dish_category_id' => string '1' (length=1)
'dish_price_label' => string '' (length=0)
2 =>
array
'id' => string '3' (length=1)
'dish_name' => string 'Empanadas de Carne' (length=18)
'dish_disc' => string 'Patties with beef' (length=17)
'dish_price' => string '1.60' (length=4)
'dish_image' => string '/img/empanadas de carne.jpeg' (length=28)
'dish_category_id' => string '1' (length=1)
'dish_price_label' => string '' (length=0)
3 =>
array
'id' => string '4' (length=1)
'dish_name' => string 'Empanadas de Pollo' (length=18)
'dish_disc' => string 'Patties with Chicken' (length=20)
'dish_price' => string '1.94' (length=4)
'dish_image' => string '/img/empanadas de pollo.jpeg' (length=28)
'dish_category_id' => string '1' (length=1)
There are more arrays besides these.. but they all have the same format.
These come from working with the following controllers
class DishCategory extends AppModel{
public $hasMany = array(
'Dish' => array(
'className' => 'Dish',
'foreignKey' => 'dish_category_id'
)
);
}
class DishCategoriesController extends AppController {
function get_categories($id)
{
// find category with a dish of $id
$this->set('dishes', $this->DishCategory->find('first', array(
'conditions' => array(
'DishCategory.id' => $id
)
)));
// set master layout
$this->layout = 'master_layout';
}
}
Use this->set('variableName', $data); to pass your arrays from the controller to the view.
You can then access the example above as $variableName in your view. I would recommend doing a <?php debug($variableName); ?> first to see what your array looks like in the view. It should be the same, but it will help you know what indices to use.
For more info: http://book.cakephp.org/view/979/set

Resources