I am using CakePHP 3 in my project and I came across of a need to format date for date_joined and date_inactivefield in my report. I can use native select query with date function to format date for the field, but in CakePHP, I am not sure how can I integrate date format in select query.
$query = $this->CustomersView->find();
$query->select(['id','contact_person',''date_joined',
'date_inactive','comments','status']);
$query->toArray();
UPDATE
I also tried one of the example from CakePHP online resource
$date = $query->func()->date_format([
'date_joined' => 'literal',
'%m-%d-%y' => 'literal'
]);
$query->select(['id','contact_person',''date_joined',
'date_inactive','comments','status']);
$query->toArray();
But it throws me error below:
'Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%m-%d-%y)) AS `datejoined`, CustomersView.date_inactive AS `CustomersView__date_' at line 1'
SQL query generated by CakePHP:
SELECT CustomersView.id AS `CustomersView__id`,
CustomersView.contact_person AS `CustomersView__contact_person`,
(date_format(date_joined, %m-%d-%y)) AS `datejoined`,
CustomersView.date_inactive AS `CustomersView__date_inactive`,
CustomersView.comments AS `CustomersView__comments`,
CustomersView.status AS `CustomersView__status`
FROM customers_view CustomersView
Any help is really appreciated. :)
Thanks,
Ray
If the date fields are of an appropriate type in your schema (e.g. DATETIME), Cake will return DateTime objects that can be formatted using plain PHP - you don't need to do it in the select.
Example:
$query = $this->CustomersView->find();
$query->select(['id','contact_person','date_joined',
'date_inactive','comments','status']);
$array = $query->toArray();
foreach ($array as $row) {
echo $row["date_joined"]->format("dMY");
}
Let's say for example that your query only returned one row, and the date_joined field here was set to 2015-12-21 23:55:00. The above code would simply print out 21Dec2015.
You can use the DATE_FORMAT($field, %d-%m-%Y) from MySQL.
Here it is an example:
$query = $this->CustomersView->find();
$query->select(['id','contact_person',''DATE_FORMAT(date_joined, "%d-%m-%Y")',
'date_inactive','comments','status']);
Fixed the problem with below code:
$query = $this->CustomersView->find();
$date = $query->func()->date_format([
'date_joined' => 'literal',
"'%m-%d-%y'" => 'literal'
]);
$query->select(['id', 'contact_person', 'date_joined' => $date,
'date_inactive', 'comments', 'status']);
$query->toArray();
I'm trying to gather data from my SQL database so I can make a custom defined table in html and insert each cell (from each row) data from a column.
I would like to input the result as a simple echo command (see example). I previously tried to do this with $i=0, then using $i++ to increment within the loop. Now when I echo the displayed data just prints 'Array'. My logic is under question so I need a second (sounder) opinion.
Connection then:
<?php
$query = "SELECT * ";
$query .= "FROM Form ";
$query .= "ORDER BY ID DESC";
$all = mysqli_query($connect, $query);
if (!$all) {
die("Database trouble.");
}
?>
<?php
while($row = $all->fetch_assoc()){
?>
then within each area of the html I wanted to insert something like:
<?php
echo $row['box1']->num_rows;
?>
then another entry,
<?php
echo $row['box1']->num_rows;
?>
etc.
<?php
}
?>
then free results and stop the connection. I can echo all the data from the table, but finding it hard to echo individual data without a long unclear script. This seems a basic requirement when echoing data, but haven't found a clear example anywhere.
Ideally I want a clean code that can be manipulated with variables from a combobox style drop down list.
Thanks in advance.
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);
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);
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);