Cakephp 3: use of "$this->alias()" in "where" conditions - php

To avoid the Column 'xxx' in where clause is ambiguous error when making queries involving multiple tables sharing the same name for a given column, I now systematically include $this->alias() in where conditions, like this :
$table->find()->where([$table->alias() . '.field' => 'value']);
Is this considered a good practice?
For some reasons I ignore, doing so leads to an error when using updateAll instead of find for tables that contains an underscore. Example:
$table = TableRegistry::get('BlogPosts');
$table->updateAll(
[$table->alias() . '.title' => 'new title'],
[$table->alias() . '.id' => 1]
);
will throw the error:
Column not found: 1054 Unknown column 'BlogPosts.id' in 'where clause' for the prepared query UPDATE blog_posts SET BlogPosts.title = :c0 WHERE BlogPosts.id = :c1
Is this a cakephp 3 bug? Or is it me who is misusing $table->alias()?

You should not alias the conditions for updateAll. The reason is that only one table is involved in that method, so the alias is not required

Related

F3 Sql Mapper: Misleading error message when setting value for non existent field in the mapped table

Given this code
$mapper->reset();
$mapper->set( 'foo', 'bar' ); // <--- Error here: `foo` does not exists in the table
$mapper->insert();
Where foo is a column that does not esist in the mapped table I get this error
Internal Server Error
SQLSTATE[42S22]: Column not found:
1054 Unknown column 'bar' in 'field list'
[/var/www/example.com/html/lib/php/fatfreeframework/DB/SQL.php:230]
The error message is misleading in fact the non existent column is foo, not bar: the latter is the value that has been attempted to set to the non-existent column.
Why does this happen? Is there a way to fix this?
-
Php 8.1.9
pdo_mysql Client API version => mysqlnd 8.1.9
mysql 8.0.30
By opening a issue on github I got the answer from #ikkez:
This is currently the expected behavior because setting a field not existing = defining an adhoc field.
$mapper->set('count_x',
'SELECT COUNT(id) from x where x.id = y.foreign_key group by x.id');
ref.: https://fatfreeframework.com/3.8/databases#VirtualFields
The way to fix this is to apply a whitelist of fillable fields that are allowed to be set in case you are using something like copyfrom.
i.e.:
$mapper->copyfrom('POST', function($val) {
return array_intersect_key($val, array_flip(['first_name', 'last_name', 'age']));
});

Laravel 5.8: SQLSTATE[42S22]: Column not found: 1054 Unknown column

I have a table called user_wallet like this:
Then at the Controller, I tried this:
try {
if ($request->pay_wallet == '1') {
$us_id = Auth()->user()->usr_id;
$user_wallet = UserWallet::find('user_id', $us_id);
dd($user_wallet);
}
}catch (\Exception $e) {
dd($e);
}
But I get this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '2' in 'field list' (SQL: select 2 from user_wallet where user_wallet.id = user_id limit 1)
However as you can see in the picture, there are two wallets with the user_id of 2.
So what's going wrong here? How can I solve this issue?
the find method only works on id so it's better to use where clause
$user_wallet = UserWallet::where('user_id', $us_id);
I think you are using find() wrong, this method accepts only one paramater, it is the value of the primary key you want to find. Because you are in a pivot table and there is no id present,
you will have to use firstWhere('user_id', $us_id) for the first occurrence or you will have to rewrite the find() method for the pivot table.
But please be aware that a user could have multiple wallets, so it might be better to ask them which wallet to use.

Laravel - using different id for unique validation

I'm using itemId instead of id and when I validate as
'barcode' => 'required|unique:item,barcode,' .$this->get('itemId'),
I get
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select count(*) as aggregate from item where barcode = 5391510260790 and id <> 10)
How do I tell Laravel to use the itemId instead of id in the unique validation in the request class at the authorize() function?
You can specify the column to check using the fourth parameter to the rule:
'barcode' => 'required|unique:item,barcode,'.$this->get('itemId').',itemId',
If you see the documentation it said, you must follow the following structure to tell which one is the id column.
unique:table,column,except,idColumn
You have to replace the idColumn with the name of the id column of yours. So the final code will be like this:
'barcode' => 'required|unique:item,barcode,' .$this->get('itemId').',itemId',

Difference between two dates in Yii's Relation

How can I perform a DATEDIFF function in CDbCriteria relations in yii
$criteria->with = array(
'company'=>array(
'condition'=>"user_type=2 AND approval_status='A'",
'select'=>'user_company'
),
'subscription'=>array(
'condition'=>"subscription_status='A'",
'select'=>'DATEDIFF(DATE(NOW()),DATE(subscription_startdate))'
)
);
I tried using the above code, Then got an error saying DATEDIFF not found in particular table.
So I add subscription relation as follows
'return array(
'company'=>array(self::BELONGS_TO,'Users',array('user_id'=>'user_id')),
'subscription'=>array(self::STAT,'SubscriptionDetails','subscription_startdate'),
);
Now also showing the below error
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found:
1054 Unknown column 'Array' in 'field list'
Is this the right way to perform date difference in yii relations?
Thanks in Advance

Laravel where_column1_and_column2 error?

I use laravel 4 PHP Framework when I write this method:
where_email_and_password($email, $password)
show this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column
'_email_and_password' in 'where clause' (SQL: select * from users
where _email_and_password = ? limit 1) (Bindings: array ( 0 =>
'email#example.com', ))
See code and error: http://i.stack.imgur.com/dn85M.png
Looks like you are migrating from Laravel 3 where this is a valid method call. In Laravel 4, everything follows the camel case convention. I have not tested this but looking at the source code for the method that handles dynamic wheres, try whereEmailAndPassword($email, $password)
See the method here: https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Query/Builder.php#L747
Hope this helps.

Categories