Saturday, June 11, 2016

Using the bean instead of SQL all the time

select * from accounts where name = 'Foo Bar';

$focus = new Account();
$focus->retrieve_by_string_fields(array('name' => 'Foo Bar' ));

$focus = new Account();
$focus->retrieve('my record id');



select * from contacts where id in ( select contact_id from accounts_contacts where account_id = 'my record id' )

$focus = new Account();
$focus->retrieve('my record id');
$focus->load_relationship('contacts');

$list = array();
foreach ($focus->contacts->getBeans() as $contact) {
    $list[$contact->id] = $contact;
}




update accounts set name = 'Bar Foo' where id = 'my record id'

$focus = new Account();
$focus->retrieve('my record id');
$focus->name = 'Bar Foo';
$focus->save();



insert into accounts_contacts (id, contact_id, account_id, date_modified, deleted) values ...

$focus = new Account();
$focus->retrieve('my account id');
$focus->load_relationship('contacts');
$focus->contacts->add('my contact id');
$focus->save();



delete from accounts_contacts where contact_id = 'my contact id' and account_id = 'my account id'

$focus = new Account();
$focus->retrieve('my account id');
$focus->load_relationship('contacts');
$focus->contacts->delete($focus->id, 'my contact id');
$focus->save();




No comments:

Post a Comment