CakePHP OR condition not properly working - cakephp

I have the following problem: I have a table for customers, with a prename and a name. Now I have variable keywords (in this example hardcoded 2) in an array and I want to check whether one of the keywords match with either the name OR the prename. So I figured to use the OR condition in the following codesnippet. Unfortunately it doesnt provide the wanted output:
$searchValueArr = ["keyword1","keyword2"];
$customers = $customersTable->find()
->where(function (QueryExpression $exp) {
$orConditions = $exp->or_(function ($or) {
foreach($searchValueArr as $searchValue) {
$or = $or
->eq('prename LIKE', "%".$searchValue."%")
->eq('name LIKE', "%".$searchValue."%");
}
return $or;
});
return $orConditions;
})
->all();

You need to list $searchValueArr in the use part of each anonymous function, otherwise it's not in context, ex:
$searchValueArr = ["keyword1","keyword2"];
$customers = $customersTable->find()
->where(function (QueryExpression $exp) use ($searchValueArr){
$orConditions = $exp->or_(function ($or) use ($searchValueArr){
foreach($searchValueArr as $searchValue) {
$or = $or
->eq('prename LIKE', "%".$searchValue."%")
->eq('name LIKE', "%".$searchValue."%");
}
return $or;
});
return $orConditions;
})
->all();
Also this is personal preference really, but you technically can still use array formatting for a query like this (a nested set of OR's), for example:
$searchValueArr = ["keyword1","keyword2"];
$searchTerms = [];
foreach($searchValueArr as $searchValue) {
$searchTerms[] = [
'OR' => [
'prename LIKE' => "%".$searchValue."%",
'name LIKE' => "%".$searchValue."%"
]
];
}
$customers = $customersTable->find()
->where([
'OR' => $searchTerms
])
->all();

Related

CakePHP Query Builder 4.x for SQL INSERT INTO IF NOT EXISTS

This CakePHP Query isn't using the conditional, $subQuery for some reason:
$subQuery = $this->queryFactory->newSelect('table_name')
->select(['id'])
->where(['id' => $id]);
$query = $this->queryFactory->newQuery()
->insert(
['id', 'machine', 'logfile', 'updated', 'time']
)
->into('table_name')
->values([
'id' => $id,
'machine' => $machine['id'],
'logfile' => $logFile,
'updated' => $updateDate,
'time' => $updateTime
])
->where(function (QueryExpression $exp) use ($subQuery) {
return $exp->notExists($subQuery);
});
$query->execute();
...it just inserts record even when it exists, but why?
The above code is only part of the required SQL that looks like this:
IF NOT EXISTS(
SELECT 1
FROM table_name
WHERE id = '$id'
)
INSERT INTO table_name (id, machine, logfile, updated, time)
VALUES (?,?,?,?,?)
ELSE
UPDATE table_name
SET updated = '$var1', time = ' $var2'
WHERE id = '$id';
There is no API that would allow to generate such a statement directly, the query builder isn't ment to generate (and execute) such SQL constructs, it can only compile SELECT, INSERT, UPDATE, and DELETE queries, and while the query expression builder can be used to stitch together arbitrary expressions, it will wrap itself and query objects into parentheses (as it is meant for use in query objects), which would be incompatible with what you're trying to build.
So if you want to run such constructs on SQL level, then you either have to write the SQL manually, or create custom expression classes that can build such constructs. In any case you would have to run the SQL manually then.
Here's a very basic quick & dirty example of such a custom expression class:
namespace App\Database\Expression;
use Cake\Database\ExpressionInterface;
use Cake\Database\ValueBinder;
use Closure;
class IfElseExpression implements ExpressionInterface
{
protected $_if;
protected $_then;
protected $_else;
public function if(ExpressionInterface $expression)
{
$this->_if = $expression;
return $this;
}
public function then(ExpressionInterface $expression)
{
$this->_then = $expression;
return $this;
}
public function else(ExpressionInterface $expression)
{
$this->_else = $expression;
return $this;
}
public function sql(ValueBinder $binder): string
{
$if = $this->_if->sql($binder);
$then = $this->_then->sql($binder);
$else = $this->_else->sql($binder);
return "IF $if $then ELSE $else";
}
public function traverse(Closure $callback)
{
$callback($this->_if);
$this->_if->traverse($callback);
$callback($this->_then);
$this->_then->traverse($callback);
$callback($this->_else);
$this->_else->traverse($callback);
return $this;
}
public function __clone()
{
$this->_if = clone $this->_if;
$this->_then = clone $this->_then;
$this->_else = clone $this->_else;
}
}
It could then be used something like this:
$notExists = (new \Cake\Database\Expression\QueryExpression())
->notExists($subQuery);
$insertQuery = $this->queryFactory->newQuery()
->insert(/* ... */)
//...
;
$updateQuery = $this->queryFactory->newQuery()
->update(/* ... */)
//...
;
$ifElse = (new \App\Database\Expression\IfElseExpression())
->if($notExists)
->then($insertQuery)
->else($updateQuery);
$binder = new \Cake\Database\ValueBinder();
$sql = $ifElse->sql($binder);
$statement = $connection->prepare($sql);
$binder->attachTo($statement);
$statement->execute();
See also
Cookbook > Database Access & ORM > Database Basics > Interacting with Statements
Yes, thanks. My own preference is to avoid the requirement to code the value binding explicitly. Using where(), I can do something like this:
$subQuery = $this->queryFactory->newSelect('table_name')
->select(['id'])
->where(['id' => $id])
->limit(1);
$find = $subQuery->execute()->fetchAll('assoc');
if (!empty($find)) {
$values = [
'id' => $id,
'machine' => $machine,
'logfile' => $logFile,
'updated' => $var1,
'time' => $var2
];
$query = $this->queryFactory->newInsert('table_name', $values);
} else {
$query = $this->queryFactory->newUpdate('table_name')
->set([
'updated' => $someVar,
'time' => $someVar2
])
->where(['id' => $id]);
}
$query->execute();

How to insert into a table based on an Eloquent relationship an array of foreign keys

I have two models TeamMember and ProjectRequest.
A TeamMember can have one ProjectRequest, that is why I created the following Eloquent relationship on TeamMember:
class TeamMember extends Model {
//
protected $table = 'team_members';
protected $fillable = ['project_request_id'];
// Relations
public function projectTeam() {
return $this->hasOne('\App\Models\ProjectRequest', 'project_request_id');
}
}
In my Controller I want to query both tables, however it returns the failure message.
What is important to know is that $request->projectTeam is an array of emails, looking like this:
array:2 [
0 => "mv#something.com"
1 => "as#something.com"
]
Meaning that I need to bulk insert into team_members table the project_request_ id for each team member where the emails are in the array.
How can I do that in the right way? The following is my attempt:
public function createProjectTeam(Request $request){
try {
$title = $request->projectTitle;
$TeamMember = $request->projectTeam;
$projectRequest = ProjectRequest::create(['project_title' => $title]);
$projectRequestId = $projectRequest->id;
$projectTeam = $this->teamMembers->projectTeam()->create(['project_request_id'=> $projectRequestId])->where('email', $TeamMember);
//$projectTeam = TeamMember::createMany(['project_request_id' => $projectRequestId])->where($TeamMember);
//dd($projectTeam);
return $projectRequest.$projectTeam;
} catch(\Exception $e){
return ['success' => false, 'message' => 'project team creation failed'];
}
}
There are a few things you can do.
Eloquent offers a whereIn() method which allows you to query where a field equals one or more in a specified array.
Secondly, you can use the update() method to update all qualifying team members with the project_request_id:
public function createProjectTeam(Request $request)
{
try {
$projectRequest = ProjectRequest::create(['project_title' => $request->projectTitle]);
TeamMember::whereIn('email', $request->projectTeam)
->update([
'project_request_id' => $projectRequest->id
]);
return [
'success' => true,
'team_members' => $request->projectTeam
];
} catch(\Exception $e) {
return [
'success' => false,
'message' => 'project team creation failed'
];
}
}
I hope this helps.

convert array to object orm entity and pass into pagination in cakephp 3

I have array that I have to pass into pagination. So how can i convert the array to Object ORM to set into pagination?
I can't $this->paginate = []; because I have to apply some foreach and conditions that's why i did first find all.
$allProductData = $this->PurchaseRequisitionProducts->find('all',[
'contain' => ['PurchaseRequisition','PurchaseOrderProducts'=>function($c){
if($c==null){ return null; }
return $c->where(['PurchaseOrderProducts.id IS NOT NULL']);
},'PurchaseOrderProducts.PurchaseOrder'=>function($p){
return $p->where(['PurchaseOrder.id IS NOT NULL'])
->where(['PurchaseOrder.is_approve'=>"Y"])
//->where(['PurchaseOrder.status'=>1])
->where(['PurchaseOrder.po_type'=>1]);
},'PurchaseOrderProducts.PurchaseOrder.CompanyMaster'] ,
'conditions'=>[$condn,$conditions,'PurchaseRequisition.ready_for_inquiry'=>'Y','PurchaseRequisition.owner_company_id'=>$ownercomp],
'order'=>["PurchaseRequisitionProducts.id"=>"desc"],
])->toArray();
if($allProductData){
foreach($allProductData as $key1=>$prProd){
if(isset($prProd['purchase_order_products'])){
$supplier=[];
foreach($prProd['purchase_order_products'] as $key2=>$poProd){
$supplier[]=$poProd['purchase_order']['supplier_id'];
//debug($supplier);
$companies= $this->CompanyMaster->find('list', [
'keyField' => 'id','valueField' => 'Company_name',
'conditions'=>['id IN'=>$supplier],
])->toArray();
$allProductData[$key1]['supplier']=$companies;
}
}
}
}
$pr_product = $this->paginate($allProductData)

Multiple collections in an array (session variable) — Property does not exist

I'am trying to fetch a session variable if the user is a guest. The variable is called "cart" and is set like this:
$product = new Collection((object) [
'product_id' => $request->pId,
'amount' => $request->amount,
'variations' => $variations
]);
Session::push('cart', $product);
Then I later fetch it:
if(Auth::check()){
$cartProducts = ShoppingCartItem::where('user_id', '=', Auth::user()->id)->get();
}else{
$cartProducts = Session::get('cart');
}
foreach($cartProducts as $product){
dd($product);
$totalAmount += $product->amount;
$totalPrice += (PriceHelper::getProductPrice($product->product->id, $product->amount));
}
The problem here is that dd($product) still outputs an array (the session variable array I assume) which means that for example $product->amount does not exist.
This is the output from dd($product):
You can either access the values using get():
foreach ($cartProducts as $product) {
$totalAmount += $product->get('amount');
$totalPrice += PriceHelper::getProductPrice($product->get('product_id'), $product->get('amount'));
}
or as an array:
foreach ($cartProducts as $product) {
$totalAmount += $product['amount'];
$totalPrice += PriceHelper::getProductPrice($product['product_id'], $product['amount']);
}
or you could use sum() on the collection instead of using foreach:
$cartProducts = collect(Session::get('cart'));
$totalAmount = $cartProducts->sum('amount');
$totalPrice = $cartProducts->sum(function ($product) {
return PriceHelper::getProductPrice($product['product_id'], $product['amount']);
});
Edit
For a quick fix if you need $product to be an object you could do something like:
$cartProducts = collect(Session::get('cart'))->map(function ($item) {
return (object)$item->toArray();
});
Hope this helps!

How to delete Model Condition?

I wrote a function which is supposed to return an array of clubs for userId. I don't know why by when I add where(Model_ClubUsers::$USERS_ID,$userId) to dsql() it doesn't set the condition and I have to use addCondition(), but I need it only in this function. Is there a way to delete the condition before function returns?
function getUserClubs($userId){
$columns = array('id',self::$NAME,self::$LOGO_NAME,self::$DESC);
$this->addRelatedEntity('club_users', 'club_users', Model_ClubUsers::$CLUBS_ID, 'inner', 'related');
$this->addField(Model_ClubUsers::$USERS_ID)->relEntity('club_users');
$aAliasedColumns = $this->getAliasedColumns($columns, $this->entity_code);
$this->addCondition(Model_ClubUsers::$USERS_ID,$userId);
$rows = $this->dsql()->field($aAliasedColumns)->do_getAll();
$aResult = array() ;
foreach($rows as $key => $value){
foreach($value as $vKey => $vVal){
if($columns[$vKey]=='id'){
$aRow['key'] = $vVal;
}else if($columns[$vKey]==self::$LOGO_NAME){
$aRow[$columns[$vKey]] = self::$CLUBS_LOGO_PATH.$vVal;
}
else {
$aRow[$columns[$vKey]] = $vVal;
}
}
$aResult[] = $aRow;
}
return $aResult;
}
Please upgrade to 4.2 where the problem is no longer present:
$x=clone $this;
$x->addCondition();
also your syntax can be probably improved with the new 4.2 features.

Resources