In odoo10 I want to insert value in many2many field onchnage depends to
many2one field:
support_id = fields.Many2one('printshop2.support', 'Print Media', required=False)
support_ids = fields.Many2many('printshop2.support.line', 'printshop_support_line_rel', 'printshop_id', 'support_id',
string='Print Media')
I do this function but it's not work:
def support_onchange(self):
printshop = self.env['offset.printshop'].browse(self.id)
allpaper = self.env['printshop2.support.line'].search([('support_id', '=', printshop.support_id.id)])
self.write({'support_ids':allpaper})
Thanks!
Try the following code, Updating many2many is done in a special way, you can not do it like other common fields.
allpaper = self.env['printshop2.support.line'].search([('support_id', '=', printshop.support_id.id)])
self.write({'support_ids':[[6, 0, allpaper]]})
Related
I'm working on laravel array serialize. Below is serialize in controller.
public function CreateSave(CreateTestTopicRequest $request){
...code..
$testtopic->class_room_id = $request->classroom;
$testtopic->roomno = serialize($request->roomno);
...code..
}
Then, roomno will be saved to database like.
a:2:{i:0;s:1:"1";i:1;s:1:"2";}
I would like to get result. For example class_room_id = 1 and roomno only contain in roomno array. I may use command to get all as below.
$testtopics = TestTopic::where('class_room_id',1)->get();
But, I do not know to get record only class_room_id = 1 and roomno contain in array. Any advice or guidance on this would be greatly appreciated, Thanks
You can use like search in json fields
TestTopic::where('class_room_id',1)->where('roomno', 'like', '%"id": 1%')->first()
When checking for an array of values the whereIn method can be used:
$roomno = 'a:2:{i:0;s:1:"1";i:1;s:1:"2";}';
$testtopics = TestTopic::where('class_room_id',1)
->whereIn('roomno', unserialize($roomno))
->get();
Multiple where statements can be combined by passing an array:
$roomno = 'a:2:{i:0;s:1:"1";i:1;s:1:"2";}';
$users = TestTopic::where([
['class_room_id', '=', '1'],
['roomno', '=', $roomno],
])->get();
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.
$projects = Project::find(collect(request()->get('projects'))->pluck('id')); // collect(...)->pluck('id') is [2, 1]
$projects->pluck('id'); // [1, 2]
I want the result to be in the original order. How do I achieve this?
Try $projects->order_by("updated_at")->pluck("id"); or "created_at" if that's the column you need them ordered by.
Referencing MySQL order by field in Eloquent and MySQL - SELECT ... WHERE id IN (..) - correct order You can pretty much get the result and have it order using the following:
$projects_ids = request()->get('projects'); //assuming this is an array
$projects = Project::orderByRaw("FIELD(id, ".implode(',', projects_ids).")")
->find(projects_ids)
->pluck('id'));
#Jonas raised my awareness to a potential sql injection vulnerability, so I suggest an alternative:
$projects_ids = request()->get('projects');
$items = collect($projects_ids);
$fields = $items->map(function ($ids){
return '?';
})->implode(',');
$projects = Project::orderbyRaw("FIELD (id, ".$fields.")", $items->prepend('id'))
->find($projects_ids);
The explanation to the above is this:
Create a comma separated placeholder '?', for the number of items in the array to serve as named binding (including the column 'id').
I solve this by querying the data one by one instead mass query.
$ids = collect(request()->get('projects'))->pluck('id');
foreach($ids as $id){
$projects[] = Project::find($id);
}
$projects = collect($projects);
$projects->pluck('id');
I have to do this manually because laravel collection maps all the element sorted by using ids.
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
Basicly i have two tables photos and users. I wanna join tables and Update colums image_max and image_min. I get error unknown colum username. In which way i can join two tabels and get data from both. My sintax is:
$this->db->select('*');
$this->db->from('photos');
$this->db->join('users', 'photos.id = users.id');
$this->db->where('username',$username);
$this->db->update('photos',$data);
And I get error
Unknown column username in where clause
UPDATE `photos` SET `image_max` = '', `image_min` = '' WHERE `username` = 'wwww'
apparently you need a letter on the table should say "users.username", check that.
Greetings.
$this->db->select('*');
$this->db->from('photos');
$this->db->join('users', 'photos.id = users.id');
$this->db->where('users.username',$username);
$this->db->update('photos',$data);
You don't need to use "select and from" before upload fields, just update in this way
$data = array('image_max'=> 4, 'image_min' => 1);
$this->db->join('users', 'photos.id = users.id');
$this->db->where('username',$username);
$this->db->update('photos',$data);