Use SugarCRM Beans to retrieve data - sugarcrm

I needed to convert this SQL query to bean format.
"SELECT first_name, last_name FROM leads INNER JOIN contacts_leads_1_c ON
leads.id=contacts_leads_1_c.contacts_leads_1leads_idb where
contacts_leads_1_c.contacts_leads_1contacts_ida='".$bean->id."'";
I have this already in place
$lead = new Lead();
$where = "contacts_leads_1_c.contacts_leads_1contacts_ida = '$bean->id' ";
$lead_list = $lead->get_full_list("", $where,true);
$all_leads = array();
foreach($all_leads as $leads){
$bean->contacts_leads = $ref->first_name . ',' . $ref->last_name;
This is the problem
$lead_list = $lead->get_full_list("", $where,true);
Thanks in advance,

If $bean is already a contact you can grab a list of the related fields:
$leads = $bean->get_linked_beans('FIELDNAME','Leads');
FIELDNAME can be found in the vardefs and isn't the relationship name it's the name of the field that defines the link.
Note: In your example you're creating an empty array then looping over it.

Related

Magento2 DOB Customer attribute Save

While customer creation , i need to store dob attribute for customer.which is not saving. facing issue like,
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'customer_entity.value_id' in 'field list', query was: SELECT customer_entity.value_id FROM customer_entity WHERE (attribute_id='11' AND entity_id='199')
meanwhile,if i added custom date attribute in customer entity it get saved.
Any one have any idea about it.
Thanks in advance.
I am storing custom attributes as given below,
$data = []; //Array of attributes with key as attribute code
$customer = $this->customerModel->load($customerId);
$customerData = $customer->getDataModel();
$customerResource = $this->customerAttrFactory->create();
foreach ($data as $attrCode => $attrValue):
$customerData->setCustomAttribute($attrCode, $attrValue);
$customer->updateData($customerData);
$customerResource->saveAttribute($customer, $attrCode);
endforeach;
No need to save dob as an attribute.
Customer entity table has dob field so we can set it through customer model as given below.
$websiteId = $this->storeManager->getWebsite()->getWebsiteId();
$customer = $this->customerFactory->create();
$customer->setWebsiteId($websiteId);
$data['dob'] = $formData['dob'];
$customer->setData($data);
$customer->save();

MongoEngine remove string from ListField

code:
class Users(db.Document, UserMixin):
first_name = db.StringField()
last_name = db.StringField()
username = db.StringField(unique=True)
password = db.StringField()
email = db.StringField()
following = db.ListField(db.StringField(), default=[])
role = db.ListField(db.StringField(), default=["user"])
confirmed = db.BooleanField(default=False)
confirmed_on = db.DateTimeField()
so if I wanted to remove a certain string from the Users.following field
Users.objects(username="some_user").update(pull__following=["some_string"])
and then save it?
because I've tried this and it won't remove the string from the following listField
If you want to remove one element from your list you need to use the pull modifier which takes a single value not a list as you are doing so the correct query is:
Users.objects(username='some_user').update(pull__following='some_string')
You can also remove several element from your "following" list using the pull_all modifier; and in this case you pass in a list of value.
Users.objects(username='some_user').update(pull_all__following=['one_string', 'another_string'])

PonyORM orphaned items when clearing a set that defines one-to-many relation

I have a basic relation defined as follows:
db = Database('sqlite', 'test_db.sqlite', create_db=True)
class WOEID(db.Entity):
woeid = PrimaryKey(int)
iso = Optional(str)
name = Required(str)
language = Optional(str)
place_type = Required(str)
parent_id = Required(int)
trends = Set('Trend')
ancestry = Optional(str)
class Trend(db.Entity):
woeid = Required(int)
events = Optional(str)
name = Required(str)
promoted_content = Optional(str)
query = Required(str)
url = Required(str)
location = Optional(WOEID)
db.generate_mapping(create_tables=True)
Now, I add some items to WOEID.trends within a function decorated with #db_session. This works as expected.
Now I try to update WOEID.trends by first reading an object using
location = WOEID.get(woeid = some_woeid)
later on I issue
location.trends.clear()
to delete old entries and I add new items to the trends set.
In the generated Trends table after this operation I have the items added, but previous items (cleared from the set) are not deleted, they stay in the database with 'location' field nulled (they are dereferenced I guess).
How should I perform the operation outlined above to get read of orphaned items?
There are two kinds of one-to-many relationships in PonyORM. The first kind of relationship is when one end of relationship is Set and the other end of relationship is Required. In that case when you remove an item from the collection this item will be deleted. For example we can define two entities Article and Comment in the following way:
class Article(db.Entity):
author = Required(User)
text = Required(str)
comments = Set('Comment')
class Comment(db.Entity):
author = Required(User)
text = Required(str)
article = Required(Article)
In that case, when you perform article.comments.clear() all comment will be deleted, because the Comment.article attribute is required and a comment cannot exist without an article.
The other kind of relationship is where Comment.article attribute is defined as Optional:
class Comment(db.Entity):
author = Required(User)
text = Required(str)
article = Optional(Article)
In that case a comment can exist without any article, and when you remove the comment from the Article.comments collection it remains in the database, but the Comment.article attribute value is set to NULL.
You can find orphaned items by executing the following query:
select(c for c in Comment if c.article is None)
or, equivalently
Comment.select(lambda c: c.article is None)
In some cases it may be desirable to define attribute as Optional, but perform cascade delete on removing item from the collection. In order to do this, you can specify cascade_delete option for the Set attribute:
class Article(db.Entity):
author = Required(User)
text = Required(str)
comments = Set('Comment', cascade_delete=True)
class Comment(db.Entity):
author = Required(User)
text = Required(str)
article = Optional(Article)
Then if you do article.comments.clear() then all removed comments will be deleted from the database.

Cayenne JOIN expression

I've been trying to do a join query with cayenne but I'm getting stuck with the use of Expressions and that.
In sql it would be something like this:
SELECT *
FROM usuarios, rol, user_rol
WHERE usuarios.cod_usuario = user_rol.cod_usuario
AND user_rol.cod_rol = rol.cod_rol
This would be the basic.
Thanks in advance
The answer to that, just like with the other question that you had is to use relationships. Once you map the relationships between Usuarios / UserRol and UserRol / Rol, you can either traverse the relationship from the user object by calling its methods:
Usuarios u = ..
for(UserRol ur : u.getUserRols()) {
Rol r = ur.getRol();
// now do something with it
}
Or find Rols with a query matching that user:
SelectQuery query = new SelectQuery(Rol.class);
query.andQualifier(ExpressionFactory.matchExp("userRol.user", u);
List<Rol> roles = context.performQuery(query);
Path argument of 'ExpressionFactory.matchExp' (i.e. "userRol.user" String) should be adjusted to match the actual relationship names starting at the Rol entity (as Rol is the root of the query), going to UserRol, and from that to Usuarios.

Doctrine 2 + Zend Form - Populate Dynamic Select Menus

I'm building a Zend form that has a dropdown/select menu populated with data from a Doctrine 2 query.
In my repository class, I have the following query in a method named selectUser():
$query = $em->createQuery('SELECT u.id, u.name FROM XX\Entity\Users u ORDER BY u.name ASC');
$users = $query->getResult();
This returns a multidimensional array, which I'm trying to loop through like this (within the same method):
$options = array();
foreach ($users as $key => $value) {
$options[$value['id']] = $value['name'];
}
return $options;
Then in my Zend form class, I try to populate the Select element like this:
$id = new Zend_Form_Element_Select('id');
$options = $this->usersRepository->selectUser();
$id->AddMultiOptions($options);
The result is an error for each user row that states "Undefined index: [name] in ...UsersRepository.php..." where [name] is the value of the 'name' column in each row.
Does anyone see what I'm doing wrong or how to populate a dynamic select menu using Doctrine 2 and Zend Framework?
(By the way, in order to run the repository method, the form class has protected properties representing the Doctrine container, entity manager, and Users repository. If this isn't considered best practice, I'd welcome any suggestions on improving my technique.)
I think your problem is here
$options[$value['id'] = $value['name']];
this would be better
$options[$value['id']] = $value['name'];