Which way is it better to call ORM methods in Odoo 7? - orm

Let's say you have in your code an object of model 'account.invoice' and inside a method you want to update the partner. I noticed that you have two ways of calling the 'write' method on model 'res.partner'. You could either do :
invoice.partner_id.write({'name': 'Mister Test'})
OR
partner_obj = self.pool.get('res.partner')
partner_obj.write(cr, uid, invoice.partner_id.id, {'name': 'Mister Test'})
I always used the second way because it is the one that is always described in documentations. However, I discovered that the first way is also working and is shorter. Is it ok to do so ?

When object is browse record than I direct write browse record object.write({'field_name': value})
invoice.partner_id.write({'name': 'Mister Test'})
This line give error because partner_id is a many2one field so it's store integer number. So you can’t use this.
For this you must to browse that partner_id and than you may to write on partner object.
And second point, if you want write something in invoice object than you can use this for example invoice.write({'field_name': value}) this will work.
Hope this make a sense.

Related

How can we make an Item in socialengine?

I know how to get an item of the particular table. Like for user we can have
$userItem = Engine_Api::_()->getItem("user", $userId);
or for a custom table
$customItem = Engine_Api::_()->getItem("custom", $customeId);
I want to know the code or method how can I make my $customItem to work the same way as $userItem works for users table. So that I can get data or manipulate the data of custom table
Thanks for your help. :-)
You can achieve that by creating a model. Check how it's done in /application/modules/User/Model. There is User.php file that declares User_Model_User and methods available for User such as getTitle, get Href etc.
You may use similar approach for your custom item. You will also need to create a file similar to /application/modules/User/Model/DbTable/User.php to declare table for your custom items.

Odoo8: create quotation from backorders

What method is responsible for the creation of quotation? i looked in sale.py but i can't find it. Indeed i want to use that method in backorders process to create a quotation from the backorders.
A quotation is a sale.order in draft state. The method that creates records for the sale.order model is the create method. Indeed this applies to all the models of Odoo/openerp. You can find this method around line 362 in addons/sale/sale.py
To create a sale order in draft mode (which is a quotation). You simply need to call the create method for the 'sale.order' model. Something like the following:
self.env['sale.order'].create({
'state':'draft',
'partner_id':
....
})
Put all the other values inside the dictionary as per your needs.

Why Ext.getCmp is a bad practice? Can be replaced with itemId in all scenarios?

I´ve been using ExtJS for a while and always used "Ext.getCmp" when instanciating components, but most of the time I read that it´s a bad practice and that "itemId" should be used instead, so I´d like to know why is "itemId" better than "Ext.getCmp" and if can be used always.
Additionally I´ve encounter examples where i´m not able (or don´t know how) to use "itemId".
Example:
I can set the disabled property of a button with "Ext.getCmp":
Ext.getCmp('btnMyButton').setDisabled(true);
But with "itemId" I get error: "object reference not set to an instance of an object"
btnMyButton.setDisabled(true);
You have to get button element by item id then call the setDisabled.
Ext.ComponentQuery.query('#btnMyButton').setDisabled(true);
You can refer below link to understand more on id vs itemId
https://vimeo.com/14816550

How to pass a variable to a model in Yii2?

I have created a beforeSave() method in one of my models to update field client_id with the client id of the requesting user ($user->client_id). However the model doesn't know who the current user is.
I could use Yii::$app->user->id but I think this is bad practice and think there must be a better way. Of course I could pass the client_id as a hidden field in the form but this would be a security issue.
Any ideas?
You could use BlameableBehavior of Yii2... client_id ..configure it as createdByAttribute or updatedByAttribute, your choice.

Grails trouble saving object id

id is present in the params but does not save to the db. for example
[book.id:1, comments: good]
comments will save to db but book.id does not.
I need to figure out how to save params.book.id into the db column BOOK_ID that was created from the hasmany/belongsTo relationship.
By default value attribute of each element will be the result of a toString() call on each element. Setting it as optionKey allows the value to be a bean property of each element in the list.
Kindly change the select to
<g:form action="review">
<g:select name="book.id"
from="${item.Book.list()}"
value="${book?.id}"
/>
<g:textField name="bookreview" value="${params?.bookreview}" /><br>
kindly refer the below link
Grail example with optionkey
You have overwritten toString, therefore your books are always rendered by name and not id. If you remove your toString() method you will see the expected results.
If you want your select box work with the name, just add optionValue="name".
Well, it seems that I had misspelled a crucial parameter inside my domain. So the domain was expecting x and it was getting y. Thus the id was always null. It took me a while to spot it, and I was perplexed because I had previously successfully implemented ids in another project. Grails has some jira issues also that make one consider other possibilities, but this time it was my mistake.