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

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);

Related

Cakephp3 how to bulk Save or Update records

Using Cake-PHP3 I am trying to bulk save or update (i.e if record is existing update, otherwise insert)
Say I have an array
$records=
[
['id'=1,'data'='data'],
['id'=2,'data'='data'],
['id'=3,'data'='data']
];
How can i bulk save or update it?
I tried using saveMany but it always results in duplicate key error if the record exist.
$entities = $this->MyModel->newEntities($records);
$this->saveMany ($entities);
I know that there is a solution using the epilog with mysql "on duplicate update"
foreach ($records AS $record) {
$query = $this->Articles->query();
$query
->insert($record_fields)
->values($record)
->epilog('ON DUPLICATE KEY UPDATE field=field+1')
->execute();
}
But that solution seems to save each record on its own and not in a bulk way which is very important to me (tons of records)
Cake\ORM\Table::saveMany($entities, $options = [])
Using this you can save multiple entities in cakephp3.
$data = []; // Your data
$entities = $this->MyTable->newEntities($data);
$result = $this->MyTable->saveMany($entities);

Display database result in a joomla module

I am trying to have the module check a table in the database and return the higest amount in the column. (using sourcerer plugin to use php in a wysiwyg module). However I just get..nothing. No error, but no returned info either.
<?php
$db = new mysqli('localhost', 'user', 'pass', 'db');
$query = 'SELECT MAX(Amount) AS Amount FROM nud9b_auction';
$result = $db->query($query);
$row = $result->fetch_row();
echo $row['Amount'];
$result->close();
?>
$db = JFactory::getDbo();
$db->setQuery('SELECT MAX(Amount) AS Amount FROM #__auction');
echo $db->loadResult();
You need not write joomla table prefix in qquery.
Full guide here:
https://docs.joomla.org/Selecting_data_using_JDatabase

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 to increase joomla article hit counter when accessing via sql?

on my joomla page i have a menu point that gives me the latest article, i wrote an extension for that. it works well, but for obvious reasons the article hit counter is not updated.
how can i fix that?
$db = JFactory::getDBO();
$query = "SELECT * FROM `#__content` WHERE `catid` = {$catid} AND `state` = 1 AND publish_up <= DATE_SUB(NOW(),INTERVAL 2 hour) ORDER BY `id` DESC LIMIT 1";
$db->setQuery($query);
$rows = $db->loadObjectList();
for the output:
echo ($rows[0]->introtext);
echo ($rows[0]->fulltext);
I recommend to use the JTable class
$table = JTable::getInstance('Content');
$table->hit($rows[0]->id);

how to use "find_in_set" in cakephp find method

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

Resources