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
Related
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.
I am trying to calculate two columns values but I am receiving the following error. The Minus between invoice and payment is a arithmetical operation. Can you please help me.
Controller.php
public function getIndex( Request $request )
{
$this->data['balanceTotal'] = \DB::table('tb_accounts')->select('sum(invoice-payment)')->get();
return view('account.index',$this->data);
}
Index.blade.php
{{ $balanceTotal }}
Error
SQLSTATE[42S22]: Column not found: 1054 Unknown column
'sum(invoice-payment)' in 'field list' (SQL: select
sum(invoice-payment) from tb_accounts)
You need a raw expression:
\DB::table('tb_accounts')->select(\DB::raw('sum(invoice)'))
But, I suppose you need to sum invoice and payment:
\DB::table('tb_accounts')->select(\DB::raw('(invoice - payment) AS amount'))
See the docs: https://laravel.com/docs/5.5/queries#raw-expressions
Note: avoid the minus in your column name or use back-ticks (Do minus sign in mysql table column names cause issues?)
this will solve your problem
use Illuminate\Support\Facades\DB;
DB::table('tb_accounts')->select(\DB::raw('(invoice + payment) AS amount'))
You are getting this error
htmlentities() expects parameter 1 to be string, array given (View: /home/unive/public_html/sistem/resources/views/accounts/index.blade.php)
because you are passing array in place of string if you need to pass array in your view try to return like this
return view('account.index')->withdata($data);
and then use in your index.blade.php
$data['balanceTotal'];
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
Route::get('/test', function(){
return DB::table('table')->where_not_null('column')->get();
});
In Laravel 4 I have the following route, obviously with table and column replaced. I had it in the controller but it wasn't working, so I moved it to a test route.
The error I am getting is:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '_not_null' in 'where clause' (SQL: select * from `table` where `_not_null` = column)
https://tower.la.utexas.edu/docs/database/fluent#where
Documentation says:
return DB::table('table')->where_null('column')->get();
It should be
return DB::table('table')->whereNull('column')->get();
Or
return DB::table('table')->whereNotNull('column')->get();
You have used where_not_null.This is the right reference.
See the proper writing for that: http://laravel.com/docs/queries#selects
DB::table('table')->whereNotNull('column')->get();
The documentation states that following the table declaration you should put the query builder information, where_null, where_not_null, where_in.
However that doesn't work. I'm not sure why, it makes no sense, and is frustrating. However, this does work.
return DB::table('user')->whereuserName('not_null')->get();
By placing the column name where the documentation says to put the query, and the query where the documentation says to put the column you are able to execute your query. To note, camel case is converted to snake case in the query, so the column has to be named user_name.
I am posting this because I attempted googling it and was not able to find anything.
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.