Case insensitive approach for sugarcrm retrieve_by_string_fields() function - php

Is there any case-insensitive approach for the bean method retrieve_by_string_fields() in sugarcrm. What we want is that when retrieving using a name Akshay and akshay should both return the same bean.
Kindly guide me on this. Thanks.

Not sure it can be done using retrieve_by_string_fields, though one solution would be to use sql to first get the bean-id, and then fetch the bean using the id.
Example using accounts module;
$name = "Akshay";
$db = DBManagerFactory::getInstance();
$id = $db->getOne("SELECT id FROM accounts WHERE name = '$name' AND deleted = 0");
if (!empty($id)) {
$account = BeanFactory::getBean("Accounts", $id);
}

Related

AppEngine DataStore - GQL not returning any results

$query = "SELECT * FROM Name";
works perfectly as expected but:
$query = "SELECT * FROM Name WHERE name = 'david'";
doesn't work as expected. The DataStore is created as follows:
$obj_name = new Entity();
$obj_name->name = strtolower($name);
$obj_name->age = $age;
$result = $obj_name_store->upsert($obj_name);
Any suggestions for how to extract a specific item using GQL?
Thank you.
It looks like you are using my php-gds library from here: https://github.com/tomwalder/php-gds
If so, then the problem is likely that you have not explicity requested the "name" property be indexed by Datastore.
When defining your Schema, you need to pass the optional second parameter "TRUE" in order for queries to work against those fields.
See here for a code example.
https://github.com/tomwalder/php-gds#defining-your-model

Limit the database access though PHP

For a plugin system I am writing, I need to write a database API. But I want to restrict database access, so plugins can't see other tables than the ones they created through a specific function. How would I enable plugins to use SQL, but not give them full access at the same time?
Here is some code, it may not be working, but it shows the idea behind it:
class Api_Database {
private $pluginid;
function __construct($pluginid) {
$this->pluginid = $pluginid;
}
function query($sql, $tablename) {
$db = new Sys_Database;
$db->query(str_replace('{table}', $pluginid.$tablename, $sql));
}
}
Am I thinking in the right direction here? How would you create such a system, only more secure?
The idea is to create a table containing the list of created tables linked to the user...
Something like :
privileges
user_id
table_name
And in your query
SELECT FROM privileges WHERE user_id = '{user_id}' and table_name = '{table}';
And after check if the row exist. If yes, the user have the right to use the table!
class Api_Database {
private $pluginid;
function __construct($pluginid) {
$this->pluginid = $pluginid;
}
function query($sql, $tablename) {
$hasPrivilege = $this->checkPrivileges($tablename, $userid);
if(!$hasPrivilege) return false; //for example
$db = new Sys_Database;
$db->query(str_replace('{table}', $pluginid.$tablename, $sql));
}
function checkPrivileges($table, $user_id) {
$db = new Sys_Database;
$result = $db->query('SELECT id FROM privileges WHERE user_id = "'.$user_id.'" AND table_name = "'.$table.'"');
return ($result && $result->num_rows);
}
}
EDIT
So if I understand correctly, you are using a PHP plugin to access SQL data, but you can't or doesn't want to change it.
You cant' add SQL users too, and you wan't to restrict PHP Dev to make some queries in some table via PHP?? Hum... Impossible!
Because to be able to disallow database table access, YOU HAVE TO MANIPULATE PHP OR MYSQL USERS...
Or if I'm wrong, sorry, it's difficult to follow you!

Fetching Collection of SugarCRM Beans

Programmatically speaking, is there a way to fetch an array or collection of SugarCRM bean objects?
That is, let's say I wanted to fetch a number of account rows that included the word Bank in their name. With raw SQL, I'd do something like this
SELECT *
FROM Accounts
WHERE name LIKE '%Associates%`;
Is there a way using the SugarCRM ORM to so something similar? If not, how do SugarCRM programmers typically handle this situation? I realize I could hack something together by selecting a list of IDs from the database
$db = DBManagerFactory::getInstance();
$result = $db->query('SELECT id FROM Accounts where name LIKE "%Banking%"');
$accounts = array();
while($row = $db->fetchRow($result))
{
$accounts[] = BeanFactory::getBean('Accounts', $row['id']);
}
but in most ORM's that would be considered inefficient, and bad practice. Is there a better way?
(Perfectly ready for the answer to be "No, there's not way to do that". I'm new to the platform and trying to get my bearings)
Rather use
$bean = BeanFactory::getBean('Accounts');
$account_list = $bean->get_full_list("", "accounts.name like '%Associates%'");
As get_list will give you what you have defined for list_max_entries_per_page.
Here is a great resource for different ways to use the standard SugarBean versus SQL: here
For your example:
$bean = BeanFactory::getBean('Accounts');
$account_list = $bean->get_list("", "accounts.name like '%Associates%'");
This is an old question but for future readers, SugarBean methods get_list() and get_full_list() seem to be deprecated and it is advised to use SugarQuery instead.
$bean = BeanFactory::getBean('Accounts');
$query = new SugarQuery();
$query->from($bean, array('team_security' => false));
$query->where()->contains('name', 'Associates');
$account_list = $query->execute();

get_where with md5()

I want to perform this simple query using Codeigniter:
$user = $this->db->get_where($type,array("id"=>$id));
$type is the name of the table and
$id is a md5() value so I want to do something like $this->db->get_where($type,array(md5("id")=>$id)); that of course is not possible to do.
the answer could be $this->db->query('select * from $type where MD5(id) = $id'); but I would prefer a more compact way like get_where() to perform query.
any suggestions?
$this->db->where('MD5(id)', $id, FALSE);
The 3rd argument when set to FALSE will stop CI from protecting your field and table names with backticks so instead of turning the code into:
WHERE `MD5(id)` = 'THE ID'
it will be:
WHERE MD5(id) = 'THE ID'
$this->db->get_where($type,array("md5(id)"=>$id)); works perfectly. I get the record I want.
To answer the original question which I came across recently and found this as the top result in Google.
$user = $this->db->get_where( $type, array( 'md5( id ) =' => $id ) );
You can also substitute = for other operators such as LIKE and NOT LIKE.

Mysql_insert_id with Doctrine

I have a couple of tables (mySQL) that i would like to update with the help of Doctrine. The products table id is auto-incrementing, and here's a brief description on what I would like to do:
$prod = new Products();
$prod->type = '0';
$categ = new CategoriesToProducts();
$categ->cat = '111';
$categ->product = $prod->id;
$conn = Doctrine_Manager::connection();
$conn->flush();
How can I do this while using flush? Using a regular save is an alternative, but there will be multiple transactions while doing such.
I have tried to find a Mysql_insert_id version for doctrine, but without any luck.
Thanks!
Here you can find some information: http://www.doctrine-project.org/documentation/manual/2_0/en/basic-mapping#identifiers-/-primary-keys

Categories