I am try to select one column from a join table but i am getting the error : Undefined index: id
$jobHistory = Report::getDb()->cache(function($db){
return Report::find()
->select('job_id')
->where(['status' => "Order Delivered"])
->andWhere(['>=', 'job_datetime', '2016-09-01'])
->andWhere(['<', 'job_datetime', '2016-09-03'])
->joinWith(['job' => function($a){
$a->select('order_placed');
}])
->asArray()
->all();
});
Related
I use zend framework 2 and I want to create this select:
SELECT
MONTH(created_at) month, COUNT(*)
FROM
requests
GROUP BY
YEAR(created_at), MONTH(created_at)
This is my function I use:
public function statPerMonth() {
$select = $this->tableGateway->getSql()->select();
$select->where(array("created_at > '2017-07-01'"));
$select->columns(array(
'month' => new \Zend\Db\Sql\Expression('MONTH(created_at)'),
'count' => new \Zend\Db\Sql\Expression('COUNT(*)')
));
//$select->group(YEAR(created_at), MONTH(created_at));
$row = $this->tableGateway->selectWith($select);
return $row;
}
How can I realize it?
Thanks for help!
SELECT
t1.post_id, t1.user_id, t1.total_likes, t1.total_comments,
t2.name,
if(t3.user_id, 1, 0) as user_like
FROM `game_posts` as t1
left join users as t2 ON t1.user_id=t2.user_id
left join(
select post_id, user_id from post_likes where user_id=1
)t3
on t1.post_id=t3.post_id
where t1.game_id=1
order by t1.post_id desc
I want to execute this query in zend framework 2, here is my code but i am enable to implement the second join having subquery in it.
$adapter = $this->tableGateway->getAdapter();
$sql = new Sql($adapter);
$select = $sql->select();
// MAIN POST TABLE
$select->from(array('t1' => 'game_posts'));
// GET POST USER
$select->join(
array('t2' => 'users'),
new Expression('t1.user_id = t2.user_id'),
array(
"user_name" => "name"
),
'LEFT'
);
$select->where(array("t1.game_id"=>$gameId));
$select->order("post_date DESC");
$statement = $sql->prepareStatementForSqlObject($select);
$results = $statement->execute();
$resultSet = new ResultSet();
$resultSet->initialize($results);
$result = $resultSet->toArray();
return $result;
Basically there are 3 tables game_posts, users and post_likes table,
i am fetching all the posts which has game_id=1 with name of the user who created the post along with the current user flag if he likes the post or not.
Please help me
There is your query :
$adapter = $this->tableGateway->getAdapter();
$sql = new Sql($adapter);
$select3 = $sql->select();
$select3->columns([post_id, user_id])->from(post_likes)->where(['user_id' => 1]);
$select = $sql->select();
$select->columns(['post_id', 'user_id', 'total_likes', 'total_comments', 'user_like' => new Expression('if(t3.user_id, 1, 0)')])
->from(['t1' => 'game_posts'])
->join(['t2' => 'users'], 't1.user_id=t2.user_id', ['name'], Select::JOIN_LEFT)
->join(['t3' => $select3], 't1.post_id=t3.post_id', [], Select::JOIN_LEFT)
->where(['t1.game_id' => $gameId])
->order('post_date DESC');
I'm trying to select columns from related models, but I'm not quite sure how to do it using Eloquent (I want to avoid the DB namespace).
This is what I have so far...
$users = User::with("role", "country") -> select(["first_name", "last_name", "email_address"]);
How would I go about modifying the select list so that it also includes role.title and country.name?
So essentially, my select list would look something like this:
-> select(["first_name", "last_name", "email_address", "role.title", "country.name"]);
Many thanks!
Did you try
User::with(['role' => function ($query) {
$query->select('title');
}])->with(['country' => function ($query) {
$query->select('name');
}])->get();
or
User::with([
'role' => function ($query) {
$query->select('title');
},
'country' => function ($query) {
$query->select('name');
}
])->get();
I am struggling with Zend now for having the joined table columns in the sql string properly.
I have this code (which works fine):
$expression = new \Zend\Db\Sql\Expression('group = acl_groups.id');
$select = $this->tableGateway->getSql()->select();
$select->columns(array(
$select::SQL_STAR,
'group',
'id',
'status'
))->join('acl_groups', 'group = acl_groups.id');
return $this->tableGateway->selectWith($select);
But I can`t get the acl_groups columns to work.
Any attempt fails. I have read the Zend Documentation and nothing effectively worked for me.
My join table has this columns:
id
name
status
I need name to have an alias "groupname"
I tried this:
$select->columns(array(
$select::SQL_STAR,
'group',
'id',
'status',
array('acl_groups.name' => 'groupname')
)
$select->columns(array(
$select::SQL_STAR,
'group',
'id',
'status',
'acl_groups.name AS groupname'
)
But none works.
You have two ways to do this as mentioned here
Method 1
$resultSet = $tableGateway->select (function (Select $select) {
// now you have `select` object, do whatever you like
});
Method 2
$select = new Select('table_name');
$select->join(
'another_table_name',
'join condition',
array('column of another table name'),
Select::JOIN_INNER
);
$resultSet = $tableGateway->selectWith($select);
I am working in rails 2, I want to execute Query
PunchingInformation.all(
:select => "users.id, login, firstname, lastname,
sec_to_time(avg(time_to_sec(punching_informations.punch_in_time))) as 'avg_pit',
sec_to_time(avg(time_to_sec(punching_informations.punch_out_time))) as 'avg_pot'",
:joins => :user,
:group => "users.id",
:conditions => {
"punching_informations.date between '#{start_date}' and '#{end_date}'",
["punching_informations.user_id IN (?)", employees.map { |v| v.to_i } ]
}
)
But it always return error like
Mysql::Error: Unknown column 'punching_informations.date between '2012-09-01' and '2012-09-25'' in 'where clause': SELECT users.id,login, firstname,lastname, sec_to_time(avg(time_to_sec(punching_informations.punch_in_time))) as 'avg_pit',
sec_to_time(avg(time_to_sec(punching_informations.punch_out_time))) as 'avg_pot' FROM punching_informations INNER JOIN users ON users.id = punching_informations.user_id AND (users.type = 'User' OR users.type = 'AnonymousUser' ) WHERE (punching_informations.date between '2012-09-01' and '2012-09-25' IN ('punching_informations.user_id IN (?)','--- \n- 28\n- 90\n')) GROUP BY users.id
Need your help.
It is a bit unclear what you meant (you have array, but taken in curly braces {} like a hash), but it seems ruby treats first string ("punching_informations.date between '#{start_date}' and '#{end_date}'") as a column, and second array, as array of expected values, thus making the invalid IN condition.
Perhaps it would work if rewritten as
:conditions => {
[ "(punching_informations.date between '#{start_date}' AND '#{end_date}') AND punching_informations.user_id IN (?)", employees.map { |v| v.to_i } ]
}
or even better
:conditions => {
[ "(punching_informations.date between ? AND ?) AND punching_informations.user_id IN (?)", start_date, end_date, employees.map { |v| v.to_i } ]
}
add punching_informations.date and punching_informations.user_id in select
:select => "punching_informations.date, punching_informations.user_id, users.id, ....