Feedback
User
This query works.Fetching datas in feedback table:
public function feedback_data()
{
$recipe_id = 0;
$this->db->where('recipe_id',$recipe_id);
$query = $this->db->get('feedback');
return $query->result_array();
}
I need to fetch feedback_id and comment from feedback table and firstname and lastname from user table where user_id from feedback is equal to user_id from user. By the way, here's my query and i think it's not working.
$this->db->select('f.feedback_id, f.comment, f.recipe_id, f.user_id as Fid'.'u.firstname, u.lastname, u.user_id as Uid');
$this->db->from('feedback f')
$this->db->join('user u','u.user_id = f.user_id');
$this->db->where('f.recipe_id', $recipe_id);
$query = $this->db->get()->result();
return $query;
Please try this :
$this->db->select('f.feedback_id, f.comment, f.recipe_id, f.user_id as Fid'.'u.firstname, u.lastname, u.user_id as Uid');
$this->db->from('feedback f')
$this->db->join('user u','u.user_id = f.user_id', 'left');
$this->db->where('f.recipe_id', $recipe_id);
$query = $this->db->get()->result();
return $query;
Try this if the above code does not work :
$this->db->select('f.feedback_id, f.comment, f.recipe_id, f.user_id as Fid'.'u.firstname, u.lastname, u.user_id as Uid');
$this->db->from('feedback f');
$this->db->join('user u','u.user_id = f.user_id', 'left');
$this->db->where('f.recipe_id', $recipe_id);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
return $query->row_array();
}
Related
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');
}
Here is my SQL Query:
SELECT
UserName,
IsApproved
FROM aspnet_Users u
INNER JOIN aspnet_Membership m
ON u.UserId = m.UserId
Can anybody help me how to write with lambda expression?
from u in aspnet_Users
join m in aspnet_Membership on u.UserId equals m.UserId
select new { u.UserName, m.IsApproved };
Try this..
from u in context.aspnet_Users
join m in context.aspnet_Membership on u.UserId equals m.UserId select new { u.UserName, m.IsApproved};
You can use the JOIN with Lambda Expression something like this,
var result = aspnet_Users
.Join(aspnet_Membership,
u => u.UserId,
m => m.UserId,
(u, m) => new
{
UserName = u.UserName,
IsApproved = u.IsApproved
}
);
A what about this query:
select UserName, IsApproved
from aspnet_Users u
inner join aspnet_Membership m on u.UserId = m.UserId
where u.UserName = #UserName
Where to put "where" in this case? #UserName is the string parameter that i define in
public ActionResult UserActivation(string UserName)
{
...
}
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.
select
count(pat.ID) as PatientEmail,
count(a.ID) as AlaEmails,
count(t.ID) as TrusteeEmails,
count(s.ID) + 1 as SpecialistEmail
from [dbo].[Users] as u
left join [dbo].[Patients] as pat
on u.ID = pat.ID and u.Email != 'email#gmail.com'
left join [dbo].[Patients] as a
on u.ID = a.ID and u.Email = 'email#gmail.com'
left join [dbo].[Trusteehips]as t
on u.ID = t.Trustee_ID
left join [dbo].[Specialists] as s
on u.ID = s.ID and u.Email != 'email#gmail.com'
where u.Active = 1 and u.Deleted = 0
public class UserEmails
{
public int PatientEmail { get; set; }
public int AlaEmails { get; set; }
public int TrusteeEmails { get; set; }
}
public ActionResult ReportByEmail()
{
var res = from u in context.Users
join p in context.Patients
on u.ID equals p.ID into pGroup
join t in context.Trusteeships
on u.ID equals t.Trustee.ID into tGroup
select new UserEmails
{
PatientEmail = pGroup.Count(g => g.Email != "email#gmail.com"),
AlaEmails = pGroup.Count(g => g.Email = "email#gmail.com"),
TrusteeEmails = tGroup.Count()
};
return View(res);
I tried to write this Linq code but it didn't work!
My count result is not shown in one row, I wrote code with same syntax (Group.Count()) and it worked fine, but here it didn't work!
can someone help me convert this sql to symfony?
SELECT cl.* from computador_coleta cl inner join class_property p on cl.id_class_property = p.id_class_property where p.id_class = 15 AND cl.id_computador = 2510;
cl.id_computador is a variable.
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
'SELECT cc FROM CacicCommonBundle:ComputadorColeta cc INNER JOIN CacicCommonBundle:ClassProperty cp WITH cc.classProperty = cp.idClassProperty WHERE cp.idClass = 15 AND cc.computador = :id'
)->setParameter('id', $computador);
$result = $query->getResult();
$em = $this->getDoctrine()->getManager();
$qb = $em->createQueryBuilder();
$result = $qb->select('c')
->from('CacicCommonBundle:ComputadorColeta','cc')
->innerJoin('cc.classProperty','cp')
->where('cp.idClass = :idClass')
->andWhere('cc.idComputador = :idComputador')
->setParameter('idClass', 15)
->setParameter('idComputador', 2510)
->getQuery()
->getOneOrNullResult();
if(!$result) {
throw new \Exception('no results')
}
I would recommend using something like this with doctrine as it is easier to read