cakephp field() query error - mysql

I am trying to extract the latest id of the table called gal_providers
//get the id of the last gal_provider add.
$order = array("GalProvider.id DESC");
$gal_provider_id = $this->GalProvider->field("id",array("order"=>$order));
$this->data["User"]["username"] = "gal_provider_".$gal_provider_id;
But it shows error like :
Warning (512): SQL Error: 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 'order = ('GalProvider.id DESC') LIMIT 1'
at line 1 [CORE/cake/libs/model/datasources/dbo_source.php, line 684]
Query: SELECT GalProvider.id FROM gal_providers AS GalProvider
WHERE order = ('GalProvider.id DESC') LIMIT 1
Whats wrong with the code ?

try to change this:
$order = array("GalProvider.id DESC");
to this:
$order = array('GalProvider.id' => 'desc');
or try this:
$gal_provider_id = $this->GalProvider->field("id",array('order' => array('GalProvider.id' => 'desc')));

As noted in the CakePHP documentation, the second argument of the Model::field() method is for a condition, and the third is for the order.
Also looking at the documentation, we can see that by default the condition and order arguments are null - which means we can do the same.
Try this:
$gal_provider_id = $this->GalProvider->field("id", null, "id DESC");
$this->data["User"]["username"] = "gal_provider_".$gal_provider_id;
In your example, you're using the Model::field() method as if it were the Model::find() method (they are two different methods and require different formats for their arguments)
Alternatively, we can use the find method itself:
$gal_provider = $this->GalProvider->find('first', array(
'fields' => array('GalProvider.id'),
'order' => 'GalProvider.id DESC',
'recursive' => -1 // more efficient (ensures no joins made for the query)
));
$this->data["User"]["username"] = "gal_provider_".$gal_provider['GalProvider']['id'];

Related

How do i use subqueries in a left join as a table

Cakephp informs me that it cannot find the table or does not recognize the alias what am i supposed to use?
Hello i am new to cake php ORM can any one tell me how to preform a left join on a subquery I'm really interested to know how to use in working the join
Here is sub query and left join so far please ignore any syntax error
$scl = TableRegistry::get('School');
$subquery = $scl->find();
$subquery->select([
'UID',
'SID',
'Total' => $subquery->func()->sum('numberOfStudents')
])->group(['UID, SID']);
$q->select([
'TeacherID',
'ClassID',
'StudentTotal' => 'sq.Total'
])->join([
'table' => $subquery,
'alias' => 'sq',
'type' => 'LEFT',
'conditions' => ['sq.UID = TeacherID', 'sq.SID = ClassID']
]);
here is the error:
[PDOException] SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sq.UID' in 'on clause'
The problem is that ORM based queries automatically alias the selected fields, so you don't get
SELECT UID, SID ...
but
SELECT UID as School__UID, SID as School__SID
hence referring to sq.UID will fail, as no such field name was selected.
To avoid this problem you can either use an alias that matches the original field name:
->select([
'UID' => 'UID',
'SID' => 'SID',
// ...
])
use the lower level database query that doesn't automatically create aliases:
$subquery = $scl
->getConnection() // connection() in older CakePHP versions
->newQuery()
->from($scl->getTable()); // table() in older CakePHP versions
or refer to the aliased fields in the main query:
'conditions' => [
'sq.' . $scl->getAlias() . '__UID = TeacherID', // alias() in older CakePHP versions
'sq.' . $scl->getAlias() . '__SID = ClassID',
]

Using MySql DATE function in CakePHP 3 query where clause

Objective: Trying to get all posts on a certain date from the db table(say, I've datetime in the format '2019-03-07 12:30:00' then I would like to get all posts from this date '2019-03-07').
As I need posts from this date, I'm converting the given datetime to i18nFormat date format. As below ::
$userSelectedDate = $selectedDate->i18nFormat('yyyy-MM-dd');
then, On the where clause, I'm using mySql DATE function on table field and it returns me expected result. code as below:
$conn = connectionManager::get('default');
$conn->begin();
$stmt = $conn->prepare(
"SELECT * FROM `blogs`
where DATE(`PUBLISHED_DATE`) = '$userSelectedDate'"
);
$stmt->execute();
$conn->commit();
This works fine. But I would like to convert it to Cakephp 3 way as below.
$query = $this
->find()
->where([
DATE($this->aliasField('PUBLISHED_DATE')) => $userSelectedDate
])
;
This obviously throws an error as below.
Error: [PDOException] SQLSTATE[42S22]: Column not found: 1054 Unknown column
How to use mysql DATE function in Cakephp 3 query? I've checked other related answers and I couldn't find a way.
Managed to find the answer.
$query = $this->find("all", [
'conditions' => [
'DATE(PUBLISHED_DATE)' => $selectedDate->i18nFormat('yyyy-MM-dd')
]
]
);

SQL Query GROUP BY Issue in Laravel Query Builder

I wrote the following MySQL query to get inbox messages of a user with each user and also with last message...
select *, `chat_channel` as `channel`, MAX(id) as max_id from `messages`
where (`message_to` = 2 or `message_from` = 2)
and (`delete_one` <> 2 and `delete_two` <> 2)
group by `channel`
order by `max_id` desc limit 40 offset 0
Laravel Method which I use...
public static function getInboxMessages($user_id, $limit = 40, $offset = 0, $search_key = null)
{
return Message::hasSearch($search_key)->select("*", DB::raw("MAX(id) as max_id"))->where(function ($sql) use (
$user_id
) {
$sql->where('message_to', '=', $user_id);
$sql->orWhere('message_from', '=', $user_id);
})->where(function ($sql) use ($user_id) {
$sql->where('delete_one', '<>', $user_id);
$sql->where('delete_two', '<>', $user_id);
})->with([
'sender' => function ($q) {
$q->select('id', 'uid', 'username', 'full_name', 'picture');
}
])->with([
'receiver' => function ($q) {
$q->select('id', 'uid', 'username', 'full_name', 'picture');
}
])->orderBy('max_id', 'DESC')->groupBy('chat_channel')->offset($offset)->limit($limit)->get();
}
However, when I run this query in phpMyAdmin I encounter the following error...
1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'db.messages.id' which is not
functionally dependent on columns in GROUP BY clause; this is
incompatible with sql_mode=only_full_group_by
When I run Laravel code directly then I don't receive any error but I do get the records ordered by as expected.
Go to phpMyAdmin -> YourDB -> SQL Tab
Use following command :
SET GLOBAL sql_mode = 'ONLY_FULL_GROUP_BY';
this will enable only full group by for your sql.
To revert the above command changes use :
SET GLOBAL sql_mode=(SELECT REPLACE(##sql_mode,'ONLY_FULL_GROUP_BY',''));
it's the STRICT MODE of mysql for aggregation.
take notice that when you group by something and you select another non aggregated field, the value of that field is not correct 100%. aggregated field are those like the fields you are grouping by, count(field), sum(field),...etc
if you are willing to take that risk, go to config\database.php and edit the strict => true to false
'prefix' => '',
'strict' => false,
'engine' => null,
it is not recommended, and you should rework your query with a join on a group by select
select x from table right join (select gb from table group by gb) as grouped on grouped.gb = table.gb
something like this

How to use AND and OR in MySQL query?

I want to get those records whose date_last_copied field is empty or less than the current date. I tried this, but it did not give me the desired result:
$tasks = $this->Control->query("
SELECT *
FROM
`controls`
WHERE
`owner_id` = ".$user_id."
AND `control_frequency_id` = ".CONTROL_FREQUENCY_DAILY."
OR `date_last_copied` = ''
OR `date_last_copied` < ". strtotime(Date('Y-m-d'))."
");
Current query looks something like this, I think. That is, find the records with the correct owner_id and frequency_id, where the date_last_copied is null or less than a certain date. Is that logic correct?
SELECT *
FROM controls
WHERE owner_id = ::owner_id::
AND control_frequency_id = ::frequency_id::
AND (
date_last_copied IS NULL
OR date_last_copied < ::date::
)
But we should really be using the CakePHP query builder, rather than running raw SQL. This article gives some details. If I were to take a stab at a solution, we'd want something like the following. But we ideally want someone from the CakePHP community to chime in here. EDIT: Note that this seems to be for CakePHP 3.0, only.
// Build the query
$query = TableRegistry::get('controls')
->find()
->where([
'owner_id' => $ownerId,
'control_frequency_id' => $frequencyId,
'OR' => [
['date_last_copied IS' => null],
['date_last_copied <' => $date]
]
]);
// To make sure the query is what we wanted
debug($query);
// To get all the results of the query
foreach ($query as $control) {
. . .
}
I'm suggesting this, rather than the raw SQL string you have above, because:
We can now leverage the ORM model of CakePHP.
We don't have to worry about SQL injection, which you're currently vulnerable to.
EDIT: OK, this is a guess at the syntax applicable for CakePHP 2.0... YMMV
$controls = $this->controls->find('all', [
'conditions' => [
'owner_id' => $ownerId,
'control_frequency_id' => $frequencyId,
'OR' => [
['date_last_copied IS' => null],
['date_last_copied <' => $date]
]
]
];
Otherwise, we just use the raw query as a prepared statement:
$result = $this->getDataSource()->fetchAll("
SELECT *
FROM controls
WHERE owner_id = ?
AND control_frequency_id = ?
AND (
date_last_copied IS NULL
OR date_last_copied < ?
)",
[$ownerId, $frequencyId, $date]
);
Not sure about your whole logic but your final query statement should be something like:
SELECT * FROM `controls` WHERE (`owner_id` = <some owner_id>)
AND (`control_frequency_id` = <some id value>)
AND (`date_last_copied` = '' OR
`date_last_copied` IS NULL OR
`date_last_copied` < CURDATE() )
Use parentheses carefully to match your logic.
Always specify the version of cakePHP you are using for your App.
This query should work fine in CakePHP 3.0 for SQL AND and OR.
$query = ModelName>find()
->where(['colunm' => 'condition'])
->orWhere(['colunm' => 'otherCondition'])
->andWhere([
'colunm' => 'anotherContion',
'view_count >' => 10
])
->orWhere(['colunm' => 'moreConditions']);

Pagination using joining multiple models

Each exam has one syllabus, each syllabus has one exam. So, I did this in the Exam model:
public $hasOne = 'Syllabuses'; //table: syllabuses, model: Syllabuses
From UsersController I did this:
public $uses = array('Setting', 'Exam', 'Syllabuses');
And in a method in UsersController I wanted to call paginate:
$options = array(
'fields' => array('Exam.id', 'Exam.name', 'Syllabuses.id', 'Syllabuses.name', 'Syllabuses.syllabus', 'Syllabuses.last_updated'),
'joins' => array(
'table' => 'syllabuses',
'conditions' => array('Exam.id = Syllabuses.exam_id')
)
);
$this->paginate = $options;
$this->set('syllabuses', $this->Paginator->paginate('Syllabuses'));
exams table:
---+------+
id | name |
---+------+
and syllabuses table:
---+---------+------+----------+--------------+
id | exam_id | name | syllabus | last_updated |
---+---------+------+----------+--------------+
So, I got some error. Like this:
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 'syllabuses Array LEFT JOIN oes.syllabuses AS Syllabuses ON (Syllabuses.`' at line 1
And my SQL that CakePHP prepared is:
SELECT `Exam`.`id`, `Exam`.`name`, `Syllabuses`.`id`, `Syllabuses`.`name`, `Syllabuses`.`syllabus`, `Syllabuses`.`last_updated`
FROM `oes`.`exams` AS `Exam` syllabuses Array
LEFT JOIN `oes`.`syllabuses` AS `Syllabuses` ON (`Syllabuses`.`exam_id` = `Exam`.`id`)
WHERE 1 = 1 LIMIT 20
But what I want is something like the query bellow. I have tested it in mysql:
SELECT `Exam`.`id` AS eid, `Exam`.`name` AS ename, `Syllabuses`.`id` AS sid, `Syllabuses`.`name` AS sname, `Syllabuses`.`syllabus` , `Syllabuses`.`last_updated`
FROM `oes`.`syllabuses` AS `Syllabuses` , exams AS Exam
WHERE Exam.id = Syllabuses.exam_id
ORDER BY Exam.id
LIMIT 20
Now anybody please help me achieve this. What kind of change can make CakePHP to prepare query like that(what I've tasted) to make my Pagination done.
Ok, I think this can be helpful for many programmers. That's why I want to share what I did finally:
$options = array(
'fields' => array(
'Exam.id',
'Exam.name',
'Syllabuses.id',
'Syllabuses.name',
'Syllabuses.exam_id',
'Syllabuses.syllabus',
'Syllabuses.last_updated'
),
'recursive' => 0,
'conditions' => array('Exam.id = Syllabuses.exam_id'),
'limit' => 3
);
$this->paginate = $options;
$syllabuses = $this->Paginator->paginate('Exam');