Eloquent query not builded as expected - sql

I have three models:
Rental.php
class Rental extends Model
{
use SoftDeletes;
public function rentalItem()
{
return $this->hasMany('App\Models\RentalItem');
}
}
RentalItem.php
class RentalItem extends Model
{
public function rentalAsset()
{
return $this->belongsTo('App\Models\RentalAsset');
}
public function rental()
{
return $this->belongsTo('App\Models\Rental');
}
}
and RentalAsset.php
class RentalAsset extends Model
{
public function rentalItem()
{
return $this->hasMany('App\Models\RentalItem');
}
}
One Rental can have many RentalItems which belongs to RentalAsset.
RentalAsset is product card for each RentalItem so one RentalItem can have one RentalAsset.
When RentalItem is created, first it needs to check if it is available in date interval. This done by checking whether between date_from and date_two is some Rental which has RentalItem related to RentalAsset.
I want a query, which will return all RentalAssets which are not related to any RentalItem being related to Rental during those dates.
Unfortunately, this is not working right:
$freeAssets = RentalAsset::where('rental_asset_category_id', '=', $request->rental_asset_category_id)
->whereDoesntHave('rentalItem.rental', function($query) use($date_from, $date_to)
{
$query->where('date_from', '<=', $date_to);
$query->where('date_to', '>=', $date_from);
})
->get();
Your help is highly appreciated!
Thanks a lot.
UPDATE:
using Laravel 5.6
UPDATE 2:
I sput out generated select by the provided eloquent query:
select * from `rental_assets` where `rental_asset_category_id` = ? and exists
(select * from `rental_items` where `rental_assets`.`id` = `rental_items`.`rental_asset_id` and not exists
(select * from `rentals` where `rental_items`.`rental_id` = `rentals`.`id`
and `date_from` <= ? and `date_to` >= ? and `rentals`.`deleted_at` is null))
and `rental_assets`.`deleted_at` is null
And this select returns what I need:
select * from `rental_assets` where `rental_asset_category_id` = 2
and not exists (
select * from `rental_items` where `rental_assets`.`id` = `rental_items`.`rental_asset_id` and exists
(select * from `rentals` where `rental_items`.`rental_id` = `rentals`.`id`
and `date_from` <= '2018-12-12' and `date_to` >= '2018-01-01' and `rentals`.`deleted_at` is null))
and `rental_assets`.`deleted_at` is null;
What is the correct eloquent query? I would rather that then raw query.
thanks.

This is probably what you need here:
$freeAssets = RentalAsset::where('rental_asset_category_id', $request->rental_asset_category_id)
->whereDoesntHave('rentalItem', function($query) use ($date_from, $date_to) {
$query->whereHas('rental', function($query) use ($date_from, $date_to) {
$query->where('date_from', '<=', $date_to);
$query->where('date_to', '>=', $date_from);
});
})->get();

Related

How to get recent conversations Laravel

I can already do this using the follow SQL command
SELECT * FROM users AS u INNER JOIN messages AS m ON u.ID IN (m.senderID, m.recipientID) WHERE u.ID <> $userID GROUP BY u.ID ORDER BY m.created_at DESC
How do I convert it to Laravel query builder, I have a Message model and User model
Do like this :
$users = User::join('messages', 'users.ID', '=', 'messages.user_id')
->whereIn('users.ID',['messages.senderId', 'messages.recipientID'])
->where('users.ID', '!=', $userID)
->groupBy('users.ID')
->orderBy('created_at','DESC')
->get();
$UserMessage = Message::where('from_user', $user_id)
->orWhere('to_user', $user_id)->latest()->get();
$usersChat = $UserMessage->map(function ($conversation) {
if ($conversation->from_user === auth()->id()) {
return $conversation->receiver;
}
return $conversation->sender;
})->unique();
return $usersChat;
and in message model
public function sender() {
return $this->belongsTo(User::class, 'from_user');
}
public function receiver() {
return $this->belongsTo(User::class, 'to_user');
}

generate mysql query from relations using via or viaTable relations

consider the below example...
class Customers extends ActiveRecord
{
public function getOrders()
{
return $this->hasMany(Orders::className(), ['customer_id' => 'id']);
}
public function getOrderItems()
{
return $this->hasMany(OrderItems::className(), ['order_id' => 'id'])
->via('orders');
}
}
how i can generate any one of the follwing query from the getOrderItems() relation
SELECT * FROM `order-items`
LEFT JOIN `orders` ON `orders`.`id` = `order-items`.`order_id`
LEFT JOIN `customers` ON `customer`.`id` = `orders`.`customer_id`
OR
SELECT `order-items`.* FROM `order-items`,`orders`,`customers`
WHERE `customer`.`id` = `orders`.`customer_id` AND `orders`.`id` = `order-items`.`order_id`
OR
SELECT * FROM `order-items` WHERE `order_id` IN(
SELECT * FROM `orders` WHERE `customer_id` IN(
SELECT * FROM `customers`
)
)
i use the following code to do this.
$customers = Customers::findAll();
$query = $customers[0]->getOrderItems()->createCommand()->rawSql;
but it only generates
SELECT * FROM `order-items`
What to do...???
use this :
$q = Customers::findAll()->innerJoinWith('orderItems')->createCommand()->rawSql;
you have to use relation name like this not as function

How to make a UNION with Doctrine?

I'm trying to do the following query:
public function findByNotifications($ownerId)
{
$em = $this->getEntityManager();
$query = $em->createQuery('
SELECT n FROM
(SELECT n FROM DelivveWebBundle:UserAd n
INNER JOIN n.ad ad
WHERE ad.owner = :ownerId
LIMIT 20
UNION
SELECT n FROM DelivveWebBundle:UserAd n
INNER JOIN n.user u
INNER JOIN n.ad ad
WHERE u.id = :ownerId
AND ad.status = :progress
LIMIT 20)
notofication
LIMIT 20;
')->setParameter('ownerId', $ownerId)
->setParameter('progress', Constant::AD_IN_PROGRESS);
$result = $query->getResult();
return $result;
}
to generate all my notifications:
public function showNotificationsAction()
{
$this->denyAccessUnlessGranted('ROLE_USER', null, 'Unable to access this page!');
$owner = $this->getUser();
$repository = $this->getDoctrine()->getRepository('DelivveWebBundle:UserAd');
$notifications = $repository->findByAdOwner($owner->getId());
return $this->render('DelivveWebBundle:Ad:notification.html.twig', array(
'owner' => $owner,
'notifications' => $notifications
));
}
The idea is to do a search on AdUser table that returns all notifications that have ads that logged User owns, along with any notifications that logged User requested.
Notification the User requested is a line of AdUser table that has the column the user logged in User.
I decided to breaking in two searches and giving a marge in results
public function findByAdOwner($ownerId)
{
$qb = $this->getEntityManager()->createQueryBuilder('n');
return $qb->select('n')
->from('DelivveWebBundle:UserAd', 'n')
->join('n.ad', 'ad')
->where('ad.owner = :ownerId')
->setParameter('ownerId', $ownerId)
->setMaxResults(20)
->getQuery()
->getResult();
}
public function findByUserNotify($userId)
{
$qb = $this->getEntityManager()->createQueryBuilder('n');
return $qb->select('n')
->from('DelivveWebBundle:UserAd', 'n')
->join('n.ad', 'ad')
->where('n.user = :userId')
->andWhere('ad.status = :status')
->setParameter('userId', $userId)
->setParameter('status', Constant::AD_IN_PROGRESS)
->setMaxResults(20)
->getQuery()
->getResult();
}
public function findNotifcations($userId){
$notification = $this->findByAdOwner($userId);
$append = $this->findByUserNotify($userId);
return array_merge($notification, $append);
}
To become more readable'll just put after something that distinguishes the two types of notice to do the treatment on the page.
I discovered that there is a way to add commands to the doctrine that does not exist, but appears to be quite complex if anyone knows do this, put the answer please.

How to make a query based on a view?

The standard way to make a query against a database table is to create a model and create a function within it , for example :
<?php
use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Query;
class Client extends Model {
function lireParCritere($critere) {
$sSQL = "
SELECT c.clt_id as clt_id,c.clt_cin_pass,c.clt_nom,c.clt_prenom,c.clt_tel,
c.clt_adresse,c.clt_comment,CONCAT_WS(
' ',
c.clt_nom,
c.clt_prenom
) AS noms
FROM Client as c WHERE 1 = 1 ";
if(isset($critere["clt_id"]) && $critere["clt_id"] != "") {
$sSQL .= "AND c.clt_id = '" . $critere["clt_id"] . "' ";
}
$sSQL .= " ORDER BY noms";
$query = new Query($sSQL,$this->getDI());
$ret = $query->execute();
return $ret;
}
}
?>
What is the way to make a query against a database view ?
A database view is basically the output of a SQL query stored in another table. You can think of a view as an alias for a specific SQL query that you can then run other queries on top of. (kind of like SELECT * FROM (SELECT * FROM table_one, table_two, table_n);)
This means that you can treat the view as a regular table and pull from it without any issues.
So lets say you have a database view that has three columns id, col_one, col_two. You could do something similar to the following.
<?php
/**
* Model representing the database view
*/
class Example extends \Phalcon\Mvc\Model
{
public $id;
public $col_one;
public $col_two;
public function getSource()
{
return 'view_name_in_database';
}
public function columnMap()
{
return array(
'id' => 'id',
'col_one' => 'col_one',
'col_two' => 'col_two'
);
}
}
// example query on the model
$examples = Example::query()
->where('id = :id:')
->bind(array(
'id' => 55
))->execute();
?>

How to calculate field in Model based on other table?

I have one model:
class Model_Alumno extends Model_Table {
public $entity_code='alumno';
function init(){
parent::init();
$this->defineAuditFields();
$this->addField('name')->caption('Nombre del Alumno')->mandatory(true);
$this->addField('is_inscrito')->type('boolean')->calculated(true);
}
function calculate_is_inscrito(){
what goes here????
}
}
I wanto to calculate is_inscrito as Y or N, if a record with it's id exists in some other table, so I can use an SQL like this:
SELECT IF( (SELECT count(*) FROM programaPago, alumno WHERE
alumno_id=CORRESPONDING ID)>0, 'Y', 'N')
How can I write the calculate_is_inscrito function?
function calculate_is_inscrito(){
return "IF( (SELECT count(*) FROM programaPago, alumno WHERE alumno_id=".
($this->table_alias?:$this->entity_code).".id)>0, 'Y', 'N')";
}