Group by function / computed field with out "store = True" - odoo-11

I want to group by computed field (type_client).
I know that I should make it store = True, but I cannot because the values ​​are dynamic. Is there another option?
This is my function:
def act_show_supect(self):
for objctf in self:
for client in self.env['res.partner'].search([('user_id.id', '=', self.user_id.id),
('company_type', '=', 'company'),
('type_client', '=', 'suspect')]):
if client.type_client == 'suspect':
objctf.ensure_one()
res = objctf.env['ir.actions.act_window'].for_xml_id(
'base', 'action_partner_form')
res.update(
context=dict(
objctf.env.context,
search_default_user_id_id=objctf.user_id.id,
search_default_type_client='suspect',
),
domain=[('user_id.id', '=', objctf.user_id.id),
('company_type', '=', 'company'),
('type_client', '=', 'suspect')]
)
return res
And this is the error after execution:
File "E:\odoo11.0\odoo\models.py", line 1908, in read_group
result = self._read_group_raw(domain, fields, groupby, offset=offset,
limit=limit, orderby=orderby, lazy=lazy)
File "E:\odoo11.0\odoo\models.py", line 1946, in _read_group_raw
assert gb_field.store and gb_field.column_type, "Fields in 'groupby'
must be regular database-persisted fields (no function or related
fields), or function fields with store=True"
AssertionError: Fields in 'groupby' must be regular database-persisted
fields (no function or related fields), or function fields with
store=True
And I want to group by:
('type_client', '=', 'suspect')

OK, let's make a small workaround:
You have to add #api.depends (the fields that you depend on with your work) and this method will run every time you change the value of the depends fields.

Related

Eloquent count occurrence with where clause

I'm trying to do a simple query using Eloquent. My test_registrants table looks like this
I want to add new column with value of all user_id with payment_status = 1
This is my query using whereColumn
TestRegistrant::select(['test_registrants.*'])
->where('payment_status', 1)
->addSelect([
'attempt' => TestRegistrant::select(DB::raw('count(*) as attempt'))
->whereColumn('test_registrants.user_id', 'user_id')
->where(function ($query) {
$query->where('payment_status', 1);
})
]);
but I get all user_id instead
What I'm trying to achieve is this one
So what do I do wrong here? thank you
The reason your query is returning 3, is because it is simply counting all the records that have payment_status = 1. The whereColumn() is not working properly, because it does not reflect the right columns.
When you define an alias for the user_id column on the test_registrants table, it should work. For example, you could name it: outer_user_id. I have updated your example accordingly:
TestRegistrant::select(['test_registrants.payment_status', 'test_registrants.user_id as outer_user_id'])
->where('payment_status', 1)
->addSelect([
'attempt' => TestRegistrant::selectRaw('count(*) as attempt')
->whereColumn('test_registrants.user_id', 'outer_user_id')
->where(function ($query) {
$query->where('payment_status', 1);
})
])
->get();
Alternatively, you could also look into grouping the results, so that you can count all the rows in a specific group.

I want to find the value who exist in array

I have 2 queries and I would like to know the elements (date_debut) of the second query which exists in the first query (dateincal). The elements of the second query can appear one or more times in the first query.
once the dates (date debut) (dateincal) have been found i want to be able to then retrieve also the other information for the element found
$feries = Jourferie::Select('dateincal', 'description')
->where('dateincal', '>=', Carbon::now()->startOfMonth())
->where('dateincal', '<=', Carbon::now()->endOfMonth())
->get();
$plannings = Planning::Select('date_debut', 'id', 'agent_id', 'site_id')
->where('id', '!=', 0)
->where('statut', 'provisoire')
->get();
I don't know if that way can help you:
foreach($feries as $ferie){
$myresult = $plannings->contains('date_debut',$ferie->dateincal)
/* do things */
}

Acess value of previous query into new nested query laravel

I'm trying to query a data using the value of the previous model, like: MyModel->whereHas(MyModel.NestedModel.value > MyModel.value)
Here is the code that I'm trying to do:
My problems is on $q->where('quantity', '<', 'inventoryItems.minimum_to_shopping');, I need a way to compare the current quantity with the quantity of previous scope.
$itemShould = InventoryItemMeta::with('inventoryItems', 'inventoryItems.lastItemValue', 'inventoryItems.inventory', 'inventoryItems.inventory.property')
->whereHas('inventoryItems', function ($q) {
$q->where('should_shopping', true)
->whereHas('lastItemValue', function ($q){
$q->where('quantity', '<', 'inventoryItems.minimum_to_shopping');
});
})->get();
You can try with the whereColumn() function, it is used to compare two columns instead that a column against a value, i.e:
$q->whereColumn('quantity', '<', 'minimum_to_shopping')

What is the equivalent query of laravel on this?

This is the native sql:
$sql = "Select count(name) from users Where email = 't#t.com' and user_id = 10";
I have this laravel code:
$checker = Customer::whereEmailAndUserId("t#t.com",10)->count("name");
Is this a correct way to do it in laravel?
You have to use where helper function and pass an array of checks. For example in your code it will be:
$checker = Customer::where([
['email', '=', 't#t.com'],
['user_id' '=', '10']
])->count();
Note: Please use the appropriate column name as it in table.
Assuming Customer model represents table users, you'll get query with eloquent like this:
Customer::where('email', 't#t.com')->where('user_id', 10)->select(\DB::raw('count(name)'))->get();
The option you are trying is incorrect
here is the right option
$users = \App\Customer::where('email','t#t.com')
->where('user_id',10)
->count()
Explanation of above code
App\Customer is the Model class and I am trying to read records where email = 't#t.com you can use various comparison operators like <,> and so on and you can also use the same function to for string pattern matching also
Eg.
$users = \App\Customer::where('email','%t.com')
->where('user_id',10)
->count()
You can use the same where function for Null Value test also
Eg.
$users = \App\Customer::where('email','=', null)
->where('user_id',10)
->count()
The above where clause will be converted to is null test of the SQL
You can read more here

How to skip a row with file exists condition in laravel

This is for a search query based on many input fields, i'm doing if statements inside the query based on the inputs, for example :
$query = Model::all();
if($field = Input::get('field'))
$query->where('column_name', $field);
but what i want to do also is a condition to skip a row if there is no image with a name of that row id, like so :
if( file_exists('/img/'.$this_query->id) )
$query->skip();
Option 1: would be to make an array of the filenames, then utilize the whereIn query builder method, which checks for the occurrence of a column value in an array.
$query = Model::query();
$filenames = scandir('/img');
// the following excludes any id's not in the $filenames array
$query = $query->whereIn('id', $filenames);
Option 2: Run the query and then filter the resulting Collection in Laravel.
$query = Model::query();
// ... then build up your query
// the following gets the query results and then filters them out
$collection = $query->get()->filter( function($item) {
return file_exists('/img/'.$item->id);
}
Rationale: the database cannot check the filesystem during it's processing; so you either need to (Option 1) provide it a list from which to check, or (Option 2) do the checking after you get() the results back from the query.

Resources