Codeigniter 4 Subquery whereIn function BaseBuilder not working - mysql

I have tried the Codeigniter 4 subquery several ways and I am now trying a method from post stackoverflow.com/questions/66795533/writing-sql-subquery-with-ci4-active-record but not working. Receiving the below error. I tried to change the type as specified below but no good.
TypeError
App\Models\Mbox\Mbox_model::App\Models\Mbox{closure}(): Argument #1 ($subqueryBuilder) must be of type App\Models\Mbox\BaseBuilder,
Below is my model and query.
namespace App\Models\Mbox;
use CodeIgniter\Model;
class Mbox_model extends Model
{
public function get_messages($uid,$box)
{
$db = \Config\Database::connect();
$builder = $db->table('messages');//->fromSubquery($subquery, 'mc');
$builder->select('messages.*,users.*,conversations.created_on,conversations.fromuser,
conversations.read, conversations.message')
->join('conversations', 'conversations.mid = messages.mid')
->join('users', 'users.id = conversations.fromuser')
->where('messages.owner', $uid)
->where('mailbox', $box)
->whereIn('conversations.cid', function (BaseBuilder $subqueryBuilder)
{
return $subqueryBuilder->selectMax('mc.cid')
->from('conversations mc')
->join('messages', 'messages.owner = mc.touser')
->where('messages.owner',$uid)
->where('messages.mailbox', $box)
->groupBy('mc.fromuser');
})->orderBy('created_on', 'DESC');

Resolved. You must include use CodeIgniter\Database\BaseBuilder;
I also had to add use the anonymous function as described in a Codeigniter forum to pass parameters and now the query looks good.
$db = \Config\Database::connect();
$builder = $db->table('messages');
$builder->select('messages.*,users.*,conversations.created_on,conversations.fromuser,
conversations.read, conversations.message')
->join('conversations', 'conversations.mid = messages.mid')
->join('users', 'users.id = conversations.fromuser')
->whereIn('conversations.cid', function (BaseBuilder $subqueryBuilder) use ($uid,$box)
{
return $subqueryBuilder->selectMax('mc.cid')
->from('conversations mc')
->join('messages', 'messages.owner = mc.touser')
->where('messages.owner', $uid)
->where('messages.mailbox', $box)
->groupBy('mc.fromuser');
})
->where('messages.owner', $uid)
->where('mailbox', $box)
->orderBy('created_on', 'DESC');

Related

How to store an element in a variable from array in Laravel?

I want to store the "area" in the $area variable from $school_info array. Using $area I want more 3 rows stored into 3 different arrays like: $school_info1, $school_info2, $school_info3.
public function schooldetailviewid($id)
{
$school_id = $id;
$school_info = DB::table('school_infos')->where('school_id', '=', $school_id)->get();
$area = $school_info->area;
// recomendation start
$school_info1 = DB::table('school_infos')->where('area', '=', $area)->get();
$school_info2 = DB::table('school_infos')->where('area', '=', $area)->get();
$school_info3 = DB::table('school_infos')->where('area', '=', $area)->get();
// recomendation end
return view('schooldetail', compact('school_info', 'school_info1', 'school_info2', 'school_info3'));
}
Problem: Storing of elements is not working and also the 3 more rows are not fetched! I am trying to make a simple recommendation system.
Error
Exception Property [area] does not exist on this collection instance.
The .get() returns an array of objects to get only one object use .first():
$school_info = DB::table('school_infos')->where('school_id', '=', $school_id)->first();
$area = $school_info->area;
Otherwise, you need to access the first element of the array:
$school_info = DB::table('school_infos')->where('school_id', '=', $school_id)->get();
$area = $school_info[0]->area;

How can I call a member function on an Array with two parameters?

in my Controller:
$relationEntity = $this->getDoctrine()->getRepository(Data::class)->findPosition($dataId,$documentId);
$relationEntity->setContent("hello");
in my Repository:
public function findPosition($dataId,$documentId) {
return $this->createQueryBuilder('data')
->leftJoin('data.documents', 'dd')
->andWhere('dd.uuid = :documentId')
->andWhere('data.document_id = :dataId')
->setParameter('documentId', $documentId)
->setParameter('dataId', $dataId)
->getQuery()
->execute();
}
I get the error message:
Call to a member function setContent() on array
I know that it would work with findOnebybut then I cannot find the entry I want to edit by two parameters. So I am not sure what to do now.
I found the solution:
foreach ($relationEntity as $r) {
$r->setContent("hello");
}
If you use custom repository you should return getResult() istead of execute, try this:
$datas = $this->createQueryBuilder('data')
->leftJoin('data.documents', 'dd')
->andWhere('dd.uuid = :documentId')
->andWhere('data.document_id = :dataId')
->setParameter('documentId', $documentId)
->setParameter('dataId', $dataId)
->getQuery()
->getResult();
return $datas[0];
If you are sure of query return only one row, you can return first result.

Want to get maximal value, but query doesn't work

I try to program a query in Yii2, which shows me the highest value of the database. Unfortunately, I get error message:
"Call to a member function all() on string"
How to patch this problem?
<?php
namespace app\controllers;
use yii\web\Controller;
use yii\data\Pagination;
use app\models\Country;
class CountryController extends Controller
{
public function actionIndex()
{
$query = Country::find();
$countries = $query->select('population')->max('population')
->all();
return $this->render('index', [
'countries' => $countries,
]);
}
}
?>
You can use this
$query = Country::find();
$countries = $query->select('population')->max('population');
Also you can use
$query = Country::find();
$countries=$query->select('max(population) as `population`')->one();
It will help you :)
You have put as 'population' or other field name in table to assign value in second query.
According to your code example, you are calling all() on the result of max(), which according to the error message is returning a string.
max() is returning the maximum value, so you probably just need to drop ... ->all().
Try this:
$countries = $query->select('population')->max('population');

How do I use the between() after find() [duplicate]

Is it possible to do a "BETWEEN ? AND ?" where condition LIKE in cakephp 2.5?
In cakephp 2.5 I write something like
'conditions' => ['start_date BETWEEN ? AND ?' => ['2014-01-01', '2014-12-32']]
how can I migrate that?
additionally I would write something like
'conditions' => [ '? BETWEEN start_date AND end_date'] => '2014-03-31']
Expressions
Between expression are supported out of the box, however they only support the first case without additional fiddling:
$Query = $Table
->find()
->where(function($exp) {
return $exp->between('start_date', '2014-01-01', '2014-12-32', 'date');
});
If you'd wanted to handle the second case via the between method, then you'd have to pass all values as expressions, which can easily go wrong, as they will not be subject to escaping/parameter binding in that case, you'd have to do that on your own (which is anything but recommended! See the security notes in the manual for PDO::quote()), something along the lines of:
use Cake\Database\Expression\IdentifierExpression;
use Cake\Database\Expression\QueryExpression;
use Cake\ORM\Query;
// ...
$Query = $Table
->find()
->where(function(QueryExpression $exp, Query $query) {
return $exp->between(
$query->newExpr(
$query->connection()->driver()->quote(
'2014-03-31',
\PDO::PARAM_STR
)
),
new IdentifierExpression('start_date'),
new IdentifierExpression('end_date')
);
});
That might feel a little inconvenient for such a basic SQL expression that is supported by all SQL dialects that CakePHP ships with, so you may have a reason here to use a raw SQL snippet with value bindig instead.
It should be noted however that expressions are often the better choice when it comes to for example cross dialect support, as they can be (more or less) easily transformed at compile time, see the implementations of SqlDialectTrait::_expressionTranslators(). Also expressions usually support automatic identifier quoting.
Value binding
Via manual value binding you can pretty much create anything you like. It should however be noted that whenever possible, you should use expressions instead, as they are easier to port, which happens out of the box for quite a few expressions already.
$Query = $Table
->find()
->where([
'start_date BETWEEN :start AND :end'
])
->bind(':start', '2014-01-01', 'date')
->bind(':end', '2014-12-31', 'date');
That way the second case can also be solved very easily, like:
$Query = $Table
->find()
->where([
':date BETWEEN start_date AND end_date'
])
->bind(':date', '2014-03-31', 'date');
A mixture of both (safest and most compatible approach)
It's also possible to mix both, ie use an expression that makes use of custom bindings, something along the lines of this:
use Cake\Database\Expression\IdentifierExpression;
use Cake\Database\Expression\QueryExpression;
use Cake\ORM\Query;
// ...
$Query = $Table
->find()
->where(function(QueryExpression $exp, Query $query) {
return $exp->between(
$query->newExpr(':date'),
new IdentifierExpression('start_date'),
new IdentifierExpression('end_date')
);
})
->bind(':date', '2014-03-31', 'date');
That way you could handle the second case using possibly portable expressions, and don't have to worry about quoting/escaping input data and identifiers manually.
Regular comparison using array syntax
All that being said, in the end BETWEEN is just the same as using two separate simple conditions like this:
$Query = $Table
->find()
->where([
'start_date >=' => '2014-01-01',
'start_date <=' => '2014-12-32',
]);
$Query = $Table
->find()
->where([
'start_date >=' => '2014-03-31',
'end_date <=' => '2014-03-31',
]);
But don't be mad, if you read all the way down to here, at least you learned something about the ins and outs of the query builder.
See also
Cookbook > Database Access & ORM > Query Builder > Advanced Conditions
API > \Cake\Database\Query::bind()
Currently there seems to be only two options. The core now supports this out of the box, the following is just kept for reference.
Value binding (via the database query builder)
For now the ORM query builder (Cake\ORM\Query), the one that is being retrived when invoking for example find() on a table object, doesn't support value binding
https://github.com/cakephp/cakephp/issues/4926
So, for being able to use bindings you'd have to use the underlying database query builder (Cake\Database\Query), which can for example be retrived via Connection::newQuery().
Here's an example:
$conn = ConnectionManager::get('default');
$Query = $conn->newQuery();
$Query
->select('*')
->from('table_name')
->where([
'start_date BETWEEN :start AND :end'
])
->bind(':start', new \DateTime('2014-01-01'), 'date')
->bind(':end', new \DateTime('2014-12-31'), 'date');
debug($Query->execute()->fetchAll());
This would result in a query similar to this
SELECT
*
FROM
table_name
WHERE
start_date BETWEEN '2014-01-01' AND '2014-12-31'
A custom expression class
Another option would be a custom expression class that generates appropriate SQL snippets. Here's an example.
Column names should be wrapped into identifier expression objects in order to them be auto quoted (in case auto quoting is enabled), the key > value array syntax is for binding values, where the array key is the actual value, and the array value is the datatype.
Please note that it's not safe to directly pass user input for column names, as they are not being escaped! Use a whitelist or similar to make sure the column name is safe to use!
Field between values
use App\Database\Expression\BetweenComparison;
use Cake\Database\Expression\IdentifierExpression;
// ...
$between = new BetweenComparison(
new IdentifierExpression('created'),
['2014-01-01' => 'date'],
['2014-12-31' => 'date']
);
$TableName = TableRegistry::get('TableName');
$Query = $TableName
->find()
->where($between);
debug($Query->execute()->fetchAll());
This would generate a query similar to the one above.
Value between fields
use App\Database\Expression\BetweenComparison;
use Cake\Database\Expression\IdentifierExpression;
// ...
$between = new BetweenComparison(
['2014-03-31' => 'date'],
new IdentifierExpression('start_date'),
new IdentifierExpression('end_date')
);
$TableName = TableRegistry::get('TableName');
$Query = $TableName
->find()
->where($between);
debug($Query->execute()->fetchAll());
This on the other hand would result in a query similar to this
SELECT
*
FROM
table_name
WHERE
'2014-03-31' BETWEEN start_date AND end_date
The expression class
namespace App\Database\Expression;
use Cake\Database\ExpressionInterface;
use Cake\Database\ValueBinder;
class BetweenComparison implements ExpressionInterface {
protected $_field;
protected $_valueA;
protected $_valueB;
public function __construct($field, $valueA, $valueB) {
$this->_field = $field;
$this->_valueA = $valueA;
$this->_valueB = $valueB;
}
public function sql(ValueBinder $generator) {
$field = $this->_compilePart($this->_field, $generator);
$valueA = $this->_compilePart($this->_valueA, $generator);
$valueB = $this->_compilePart($this->_valueB, $generator);
return sprintf('%s BETWEEN %s AND %s', $field, $valueA, $valueB);
}
public function traverse(callable $callable) {
$this->_traversePart($this->_field, $callable);
$this->_traversePart($this->_valueA, $callable);
$this->_traversePart($this->_valueB, $callable);
}
protected function _bindValue($value, $generator, $type) {
$placeholder = $generator->placeholder('c');
$generator->bind($placeholder, $value, $type);
return $placeholder;
}
protected function _compilePart($value, $generator) {
if ($value instanceof ExpressionInterface) {
return $value->sql($generator);
} else if(is_array($value)) {
return $this->_bindValue(key($value), $generator, current($value));
}
return $value;
}
protected function _traversePart($value, callable $callable) {
if ($value instanceof ExpressionInterface) {
$callable($value);
$value->traverse($callable);
}
}
}
You can use one of following 2 methods.
Method 1 :
$start_date = '2014-01-01 00:00:00';
$end_date = '2014-12-31 23:59:59';
$query = $this->Table->find('all')
->where(function ($exp, $q) use($start_date,$end_date) {
return $exp->between('start_date', $start_date, $end_date);
});
$result = $query->toArray();
Method 2:
$start_date = '2014-01-01 00:00:00';
$end_date = '2014-12-31 23:59:59';
$query = $this->Table->find('all')
->where([
'start_date BETWEEN :start AND :end'
])
->bind(':start', new \DateTime($start_date), 'datetime')
->bind(':end', new \DateTime($end_date), 'datetime');
$result = $query->toArray();
I'm using it like this
$this->Table->find()->where(['data_inicio BETWEEN '.'\''.$data_inicio.'\''.' AND .'\''.$data_final.'\''.' ']);
Hello guys please use this query to get data on the basis of range of value
$query = $this->Leads->find('all',
array('conditions'=>array('postcode BETWEEN '.$postcodeFrom.' and'.$postcodeTo.''), 'recursive'=>-1));
debug($query);
print_r($query->toArray());

Chaining orX in Doctrine2 query builder

I have to dynamically add OR expressions to the query builder returned by getListQueryBuilder, right after adding a where clause. I can't find any suitable way of doing this, i'm just started learning Doctrine.
How can i "chain" a given number of orX and add them to my builder?
public function getListQueryBuilder($ownerId)
{
$qb = $this->createQueryBuilder('t');
return $qb
->where($qb->expr()->eq('t.user', ':user'))
->setParameter('user', $ownerId);
}
$builder = getListQueryBuilder(4);
// $ORs is a dynamically builded array, here is just an example
$ORs = array();
$ORs[] = $builder->expr()->like("t.name", 'my name');
$ORs[] = $builder->expr()->like("t.description", 'desc');
// Adding ORs to the builder
$builder->andWhere($builder->expr()->orX(/* here */));
You can check this solution:
$orX = $builder->expr()->orX();
foreach($ORs as $or) {
$orX->add($or);
}
$builder->andWhere($orX);
I stumbled on the same problem and tried :
$builder->andWhere($builder->expr()->orX($ORs));
but it does not work since orX calls "return new Expr\Orx(func_get_args());" internally and you end up with something like array(array(or1, or2))
having looked at the API however i figured you can do this:
$builder->andWhere($builder->expr()->orX()->addMultiple($ORs));
OR do use $ORs table at all but issue:
$orx = $builder->expr()->orX();
$orx->add($builder->expr()->like("t.name", 'my name'));
$orx->add($builder->expr()->like("t.description", 'desc'));
$builder->andWhere($orx);