How to add join columns to tablegateway in zendframework 2 - join

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);

Related

How to group by with Zend Db

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!

Running subquery in zend framework 2 with if condition

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');

select a column from join table active query yii2

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();
});

zend insert sql not work

trying to understand zf2.
my code:
$sql = new Sql($dbAdapter);
$insert = $sql->insert('security');
$insert->values(array(
'user' => $userName,
'ip' => '',
'result' => 2
));
$dbAdapter->query($insert->getSqlString(), $dbAdapter::QUERY_MODE_EXECUTE);
error:
Notice: Attempting to quote a value without specific driver level
support can introduce security vulnerabilities in a production
environment. in
/opt/projects/my/newSymbio/current/vendor/zendframework/zendframework/library/Zend/Db/Adapter/Platform/Sql92.php
on line 80
any ideas?
Here is the right syntax of insert in zf2:
$data = array(
''user' => $userName,
'ip' => '',
'result' => '2'
);
$this->tableGateway->insert($data);
and you need to get the object of table in which table to want to insert the data.
Or another way as you are using is:
use Zend\Db\Sql\Sql;
Check you are using above line. And replace your last line with following code:
$selectString = $sql->getSqlStringForSqlObject($insert);
$results = $this->dbAdapter->query($selectString, $dbAdapter::QUERY_MODE_EXECUTE);
And I think you need to call change the $dbAdapter to $this->dbAdapter also try this.
i figure it out.
$sql = new Sql($dbAdapter);
$insert = $sql->insert('security');
$insert->values(array(
'user' => $userName,
'ip' => '',
'result' => 2
));
$statement = $sql->prepareStatementForSqlObject($insert);
$results = $statement->execute();

Laravel Update Syntax - Updating Record with Array

I'm having trouble doing a basic record update via POST in Laravel.
I have captured all the post data in an array, and if the existing Order# is 0, then I create a new record (works fine). Otherwise I update the existing record.
Order.php
class Order extends Eloquent {
public static $table = 'my_orders';
}
Routes.php
//Handle a new order POST
Route::post('order', array('do' => function() {
$thisOrder = array(
'qty' => Input::get('quantity'),
'desc' => Input::get('description'),
);
$thisOrderID = Input::get('orderNo');
//CHECK FOR NEW OR EXISTING ORDER
if($thisOrderID > 0) {
//THIS FUNCTION SOMEHOW RETURNS THE FUNCTION CALL AND DOESNT CONTINUE PAST
//AND THE RECORD IS NOT UPDATED
$updateOrder = Order::update($thisOrderID, $thisOrder);
}
}
Update:
The code above does in fact work. I had a validation error, which was causing the function to return early.
Instead of this line:
$updateOrder = Order::update($thisOrderID, $thisOrder);
You need to do:
$updateOrder = Order::find($thisOrderID)->update($thisOrder);
With find() (which equals where_id()) you select a specific row from the database and with update you pass the new data.
What do you think of this:
//Handle a new order POST
Route::post('order', array('do' => function() {
$thisOrder = array(
'qty' => Input::get('quantity'),
'desc' => Input::get('description'),
);
$thisOrderID = Input::get('orderNo');
$order = Order::find( $thisOrderID );
//CHECK FOR NEW OR EXISTING ORDER
if( $order ) {
$order->title = 'New order title';
$order->desc = 'New description';
$order->save();
}
}
erm , I will suggest to do it this way, if $thisOrder include the key, it will just update the record, else it will just create a new record.
$thisOrder = array(
'orderNo' => Input::get('orderNo'),
'qty' => Input::get('quantity'),
'desc' => Input::get('description'),
);
$updateOrder = Order::save($thisOrder);
if Input::get('orderNo') no value will be create else update

Resources