how to use "find_in_set" in cakephp find method - cakephp

I have a table in which comma seprated id of another table i want to use the following query in cakephp in proper form with find function
"select * from special_offers where find_in_set('".$storeId."', stores) and user_id = '". $userId ."'";

Use like this
$data = $this->SpecialOffer->find('all',array('conditions' => array('SpecialOffer.user_id' => $userId ,'FIND_IN_SET(\''. $storeId .'\',SpecialOffer.stores1)')));
Hope this may help you

Related

Reduce arrays where values are equals

It's a simple question I think, but i can't handle this.
If you see the image, here is my DB with multiple Usernames and "instances_id". If I var_dump this I have 11 arrays (and that's normal).
Is there a way to reduce arrays where "instance_id" is equal?
So that I have the first array containing "Alessio,Giorgio", the second one "Alessio,Giovanni" etc.
I use laravel... I hope you can help me
You could use combination of GROUP BY with GROUP_CONCAT to retrieve directly from db:
$result = DB
::table('example')
->selectRaw('instance_id, GROUP_CONCAT(Username)')
->groupBy('instance_id')
->get();
Or via laravel collection:
$result = DB::table('example')->get();
$result = $result
->groupBy('instance_id')
->map(function($group){
$data = $group->first();
$data->Username = $group->pluck('Username')->implode(',');
return $data;
})
->values();
Try..
SELECT * FROM tableName GROUP BY fieldName;
The following query will select all fields along with distinct zip field.
There are two ways.
1)select *
from table
group by field1
2) select distinct on field1 *
from table

CAKEPHP 3 : Select * and sum() in one statement

I am trying to fetch result from database table with SELECT * and SUM() function.
The sql query is :
SELECT * ,SUM(msg_send) AS msg_send FROM msg_campaigns
Now how to write this query in cakephp3.
I am trying this :
$this->loadModel('MsgCampaigns');
$SmsDetails = $this->MsgCampaigns->find('all',[
'conditions'=>['YEAR(date_time)'=>date('Y')],
'fields'=>['msg_send'=>'SUM(msg_send)','msg_failed'=>'SUM(msg_failed)']
]);
But I do not know how to use SELECT * . Please help
Check the CakePHP Query Builder on how to use SQL functions and how to select all fields.
$query = $this->MsgCampaigns->find();
$query
->select([
'sum_msg_send' => $query->func()->sum('msg_send'),
'sum_msg_failed' => $query->func()->sum('msg_failed')
])
// passing the table instance to the `select` function, selects all fields
->select($this->MsgCampaigns);
$query->execute();

Cake 2.X retrive data from rawQuery

As show at CakePHP 2.1.x - Run a query without any models in AppController
I have a query,
$q = "select id from table where id=123";
$db = ConnectionManager::getDataSource('default');
$qr = $db->rawQuery($q);
ok (!), it works... But, how to get my data?? Where the tutorial examples?
I need something like $data = $qr->fetchAll() method or $id = getMyData($qr) function.
I believe this may be the solution or at least a point in the right direction.
$q = "select id from table where id=123";
$db = ConnectionManager::getDataSource('default');
$myData = $db->query($q);

How do you use delete, update, insert in joomla mvc from the file you create

I have been able to create simple MVC app to display the form and use information from the database. [ so sql I use SELECT ].
However, when I use 'update' or 'delete' the data in the database stay the same. I don't understand!?!
[ I use controller.php call model and pass id to match with the row in the table]
I check data that pass in there, and it did echo the value of id.
The page I load from the controller that call method in model has complete without the error (it could be that the syntax is right, but logic is wrong).
so i start to change sql statement to SELECT and it gives me the value that match with $id param'
I don't understand? any clue?
$query = JFactory::getDbo()->getQuery(true);
$db =& JFactory::getDBO();
$query = "DELETE FROM `menutables` WHERE `id2` = $id ";
$db->setQuery($query);
$db->query(); // this line I also take it out to test, it is fine but no data change either
Do you try to modify a Joomla database or a personal one?
Look at this pages :
http://docs.joomla.org/How_to_connect_to_an_external_database
http://docs.joomla.org/How_to_use_the_database_classes_in_your_script#Tips.2C_Tricks_.26_FAQ
try to use either
$query = "DELETE FROM #__menutables WHERE id2 = '$id' ";
or
$query = "DELETE FROM #__menutables WHERE id2 = '".$id."' ";
it'll work fine then.
Deleting a Record
delete query in Joomla 2.5.
$db = JFactory::getDbo();
$query = $db->getQuery(true);
// delete all custom keys for user 1001.
$conditions = array(
$db->quoteName('user_id') . '=1001',
$db->quoteName('profile_key') . '=\'custom.%\''
);
$query->delete($db->quoteName('#__user_profiles'));
$query->where($conditions);
$db->setQuery($query);

How to use LIKE OR operator using cakephp & mysql

I am new to cakephp & don't know what is the syntax to use LIKE & OR operator in cakephp with mysql.
Can anyone help me?
Thanks..
Complex find conditions from the manual:
$this->Post->find('first', array (
"Author.name" => "Bob",
"OR" => array (
"Post.title LIKE" => "%magic%",
"Post.created >" => date('Y-m-d', strtotime("-2 weeks"))
)
));
you can use:
for "like"
$this->Post->find("all",array('condition'=>array('Author LIKE'=>"ad%")));
above query will give You the data from table posts where author name starts with "ad".
for "OR"
$this->Post->find("all",array('condition'=>array("OR"=>array('Author LIKE'=>"ad%",'Post LIKE'=>"bo%"))));
above query will give You the data from table posts where author name starts with "ad" OR Post starts with "bo".
if you using where function then use this :-
->where(['Products.category_id'=>1, 'Products.name LIKE' =>'test%'])
thanks

Resources