Cakephp3 query with contain returns 0 results - sql

Here is my query that works:
$query = $appointments->find();
$query->where(['Appointments.id =' => '786']);
$query->order(['date' => 'ASC']);
This returns 1 result. Now when I want to tie in the other 'belongs to' tables I use this query:
$query = $appointments->find();
$query->select(['Appointments.id', 'Appointments.start', 'Clients.name', 'Services.Name']);
$query->where(['Appointments.id =' => '786']);
$query->contain(['Clients', 'Users', 'Services']);
$query->order(['date' => 'ASC']);
This returns 0 results, am I doing something wrong?
Here is my my AppointmentsTable.php:
public function initialize(array $config)
{
parent::initialize($config);
$this->table('appointments');
$this->displayField('id');
$this->primaryKey('id');
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Clients', [
'foreignKey' => 'client_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Services', [
'foreignKey' => 'service_id'
]);
}

Related

How to make array taking data from database in laravel

why this is not working where all my queries are individually working.
$data = [
'name' => $user->name,
'email' => $user->email,
'phone' => $profile->phone,
'address' => $profile->address,
'gender' => $profile->gender,
];
return $data;
this works manually like
$data = [
'name' => 'my_name',
'email' => 'my_email',
'phone' => 'my_phone',
'address' => 'my_address',
'gender' => 'my_gender',
];
return $data;
my whole function is given below:
public function read($id){
$user=DB::table('users AS t1')
->select('t1.name','t1.email')
->where('t1.id',$id)->get();
$profile=DB::table('profiles AS t1')
->select('t1.phone','t1.gender','t1.occupation','t1.address')
->where('t1.user_id',$id)->get();
$data = [
'name' => $user->name,
'email' => $user->email,
'phone' => $profile->phone,
'address' => $profile->address,
'gender' => $profile->gender,
];
return $data;
When using get() it returns a collection not a single object so you can do
public function read($id){
$user=DB::table('users AS t1')
->select('t1.name','t1.email')
->where('t1.id',$id)->first();
$profile=DB::table('profiles AS t1')
->select('t1.phone','t1.gender','t1.occupation','t1.address')
->where('t1.user_id',$id)->first();
$data = [
'name' => $user->name,
'email' => $user->email,
'phone' => $profile->phone,
'address' => $profile->address,
'gender' => $profile->gender,
];
return $data;
Or if you have relations defined on the models you can use the relation
class User extends Model
{
public function profile()
{
return $this->hasOne(Profile::class);
}
}
class Profile extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
}
public function read($id)
{
$user = User::with('profile')->findOrFail($id);
$data = [
'name' => $user->name,
'email' => $user->email,
'phone' => $user->profile->phone,
'address' => $user->profile->address,
'gender' => $user->profile->gender
];
return $data;
}

Error message : Unknown column 'a.id_configuration' in 'order clause'

With this code i would like print a tab with some fields in prestashop back office. But i got an error message: : Unknown column 'a.id_configuration' in 'order clause'
<?php
public function __construct()
{
parent::__construct();
$this->table = 'order_invoice'; // SQL table name, will be prefixed with _DB_PREFIX_
$this->className = 'OrderInvoice'; // PHP class name
$this->allow_export = true; // allow export in CSV, XLS..
$this->fields_list = [
'id_order_invoice' => ['title' => $this->trans('ID', [], 'Admin.Global'),'class' => 'fixed-width-xs'],
'number' => ['title' => $this->trans('Number', [], 'Admin.Global'),'class' => 'fixed-width-xs'],
'date_add' => ['title' => $this->trans('Date', [], 'Admin.Global'), 'type'=>'datetime'],
'total_products_wt' => ['title' => $this->trans('Total products', [], 'Admin.Global'),
'align' => 'text-right',
'type' => 'price',
],
'total_shipping_tax_incl' => ['title' => $this->trans('Total shipping', [], 'Admin.Global'),
'align' => 'text-right',
'type' => 'price',
],
'total_paid_tax_incl' => ['title' => $this->trans('Total paid', [], 'Admin.Global'),
'align' => 'text-right',
'type' => 'price',
],
];
}
?>
```
Since it states it's an error within the order clause (ORDER BY), we'd edit the orderBy variable like this:
$this->_orderBy = 'id_order_invoice';
You can also edit the identifier since it seems to be using the wrong identifier to begin with:
$this->identifier = 'id_order_invoice';
Hope this helps.

Relation two database and make join yii2

Can you help me. How to relate it and make a join
. i get error has no relation named "project".
i using ActiveRecord with my code :
$posts = MaKantor::find()
->leftJoin('project.track', '`track`.`id_office` = `m_kantor`.`kantor_id`')
->with('project.track')->where(['collecting_id' => $model->collecting_id])
->all();
and config
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=project',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
'db2' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=db_master',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
When you use with('relationName') in query, relation function needs to be defined in MaKantor model.
For example :
public function getProject()
{
return $this->hasOne(Project::className(), ['id' => 'project_id']);
}

Yii2- ArrayHelper change index of array

I have an array as follows:
[
0 => [
'name' => 'CARD'
'id' => '0'
]
1 => [
'name' => 'MOBILE'
'id' => '1'
]
2 => [
'name' => 'GIFT'
'id' => '2'
]
]
I want to change the key id to type in all the array. Is there a way to do this in Yii2 using ArrayHelper?
You can use getColumn() for this:
$result = ArrayHelper::getColumn($array, function ($data) {
return [
'name' => $data['name'],
'type' => $data['id'],
];
});
But it will not really differ from array_map() or simple foreach.
There is not an array helper for this but you could do this with a php foreach
foreach ($myArray as $key => $value) {
$myArray[$key]['type'] = $value['id'];
unset($myArray[$key]['id']);
}

Joining With SQL Function In CakePHP 3

Hi there I want to change this query into cakePHP 3.0 format but the code is not working could you guys can help me to solve this problem?
Normal SQL:
SELECT COUNT(user_tag_review.id) AS user_numbers, SUM(user_rating.rating_value) AS rating_value FROM user_tag_review INNER JOIN user_rating ON user_tag_review.id = user_rating.review_id WHERE user_tag_review.recruiter_id = 2 AND user_rating.rating_id = 1
CakePHP code that I have used:
$review_table = TableRegistry::get('UserTagReview');
$query = $review_table->find();
$query->select(["user_numbers" => $query->func()->count("UserTagReview.id")])
->hydrate(false)
->join(["table" => "user_rating",
"alias" => "r",
"type" => "INNER",
"condition" => ['r.review_id = UserTagReview.id']
])
->select(["rating_value" => $query->func()->sum("r.rating_value")])
->where(["UserTagReview.recruiter_id" => $recruiter_id, "UserTagReview.rating_id = " . 1]);
$result = $query->all()->toArray();
I think simpler way to migrate that SQL to Cake is fields options in finder:
$table = TableRegistry::get('UserTagReview');
$result = $table
->find('all' , [
'fields' => [
'user_numbers' => 'COUNT(UserTagReview.id)',
'rating_value' => 'SUM(user_rating.rating_value)'
],
'join' => [
[
'table' => 'user_rating',
'alias' => 'user_rating',
'type' => 'INNER',
'conditions' => [
'UserTagReview.id = user_rating.review_id'
]
]
],
'conditions' => [
'UserTagReview.recruiter_id' => 2,
'user_rating.rating_id' => 1
]
])
->hydrate(FALSE)
->toArray();
Maybe this code is not 100% correct, but focus on fields array - if we use this as associative array, we can evaluate any sql:
'fields' => [
'alias' => 'COUNT(IF(1,1,0))'
]