ZF2 Inner Join on other timestamp - php

ZF2 Inner Join on other timestamp
Hello everybody, I would like to detect poitive edges in da Database with Zend Framework 2 (ZF2).
In mySQL it works with this command:
SELECT Ta1.datetime, Ta1.SensorId_47, Ta0.SensorId_47
FROM data_newobjects Ta1
INNER JOIN data_newobjects Ta0
ON Ta1.datetime = Ta0.datetime + SECOND(10)
WHERE (Ta1.SensorId_47 LIKE "1%" AND Ta0.SensorId_47 LIKE "0%");
But how can i join on the next timestamp like here the "+ SECOND(10)" in ZF2?
It works, if I join on the same timestamp in ZF2:
$sm = $this->getServiceLocator();
$sql =new Sql($sm->get('adapter_rohdata'));
$exp_Text = "`Ta_1`.`datetime` AS `summe`";
$select = $sql->select();
$select->columns(array(new Expression($exp_Text)));
$select->from(array('Ta_1' => 'data_newObjects'));
$select->join(array('Ta_0' => 'data_newObjects'), 'Ta_1.datetime=Ta_0.datetime', array(), \Zend\Db\Sql\Select::JOIN_INNER);
$select->where->like('Ta_0.SensorId_47', "0%");
$select->where->like('Ta_1.SensorId_47', "1%");
$statement = $sql->prepareStatementForSqlObject($select);
$results = $statement->execute();
But I didn't find a solution, for the "next" timestamp, like it worked in mySQL for instance with "Ta1.datetime = Ta0.datetime + SECOND(10)"
Thanks for help... I really searched for help, and tryed a lot...

Got the solution from this post:
$select->join(
array('Ta_0' => 'data_newObjects'),
new \Zend\Db\Sql\Expression('Ta_1.datetime = Ta_0.datetime + SECOND(10)'),
array()
);

Related

Zend Framework 2 join the same table multiple times using alias

I'm struggling to implement multiple left JOINs into ZF2. I've got first one working, but when I add another one, it doesn't work.
This is the working SQL query which I should implement into zf2:
SELECT
ac.ctr_id AS ctr_id,
ac.ctr_no AS ctr_no,
ac.ctr_marketer AS marketer,
ac.ctr_manager AS manager,
ac.ctr_recruiter AS recruiter,
l1.emp_realname AS marketer,
l2.emp_realname AS co_recruiter_manager,
l3.emp_realname AS recruiter
FROM
allcontracts AS ac
JOIN
lstemployees AS le ON ac.ctr_recruiter = le.emp_id
LEFT JOIN
lstemployees AS l2 ON ac.ctr_manager = l2.emp_id
LEFT JOIN
lstemployees AS l3 ON ac.ctr_recruiter = l3.emp_id
LEFT JOIN
lstemployees AS l1 ON ac.ctr_marketer = l1.emp_id
from my model:
.....
$where = new Where();
$this->table='allcontracts';
$select = new Select($this->table);
$select->columns(array('*')); // TODO add columns from allcontracts table
// This one works
$select->join('lstemployees', 'allcontracts.ctr_recruiter = lstemployees.emp_id');
// When I add this one below it doesn't work
$select->join(array('l2' => 'lstemployees'), 'allcontracts.ctr_manager = l2.emp_id', array('*'), 'left');
$where->like('ctr_no', '%LT');
if($id!='' && $id > 0)
$where->equalTo('ctr_id', $id);
$select->where($where);
$resultSet = $this->selectWith($select);
......
Any idea?
Here is what I propose:
<?php
use Zend\Db\Sql\Select;
$select = new Select();
$select->columns([Select::SQL_STAR])
->from(['ac' => 'allcontracts '])
->join(['le' => 'lstemployees'], 'ac.ctr_recruiter = le.emp_id', [])
->join(['l1' => 'lstemployees'], 'ac.ctr_marketer = l1.emp_id', ['marketer' => 'emp_realname'], Select::JOIN_LEFT)
->join(['l2' => 'lstemployees'], 'ac.ctr_manager = l2.emp_id', ['co_recruiter_manager' => 'emp_realname'], Select::JOIN_LEFT)
->join(['l3' => 'lstemployees'], 'ac.ctr_recruiter = l3.emp_id', ['recruiter' => 'emp_realname'], Select::JOIN_LEFT);
// to debug your query
die($select->getSqlString($dbAdapter->getPlatform()));
// if you haven't $dbAdapter, replace by null but the result will be quoted.

Zend Update Query

How to write below mysql query using zend syntax?
UPDATE core_user_transaction as t
JOIN core_user_transaction_therapy AS cuts ON cuts.tid=t.tid
JOIN therapy_booking AS tb ON cuts.conf_id = tb.conf_id
JOIN therapy_service_fees AS fees ON cuts.fee_id=fees.id
JOIN therapy_service_taxes AS tst ON fees.service_id=tst.service_id
JOIN core_tax AS ct ON ct.tax_id = tst.tax_id
SET t.tax=ROUND(t.fee*(ct.tax_value/100),2),
t.amount = (ROUND(t.fee*(ct.tax_value/100),2)+t.fee)
WHERE tb.datetime >= '2015-10-01 00:00:00'
AND t.state = 'PENDING'
AND t.status ='ACTIVE'
Try this:
$adapter = new Zend\Db\Adapter\Adapter($driverConfig);
$sql = "YOUR_QUERY";
$statement = $adapter->query($sql);
$statement->execute();
Also please refer Zend\Db\Adapter
There is no possible with the 'update()' method provided by the Zend Db Adapter.
You can however, run the query manually using the adapter. Something like:
// $adapter is an instance of Zend_Db_Adapter
$adapter->query(YOUR QUERY HERE);

Zend Database Mutli table "Select" via Join

Using Zend DB. I am trying to figure out how to write this query using the DB Class.
select
org.orgid
org.role
user.userid
user.firstname
from orgTable org
join userTable user on org.userid = user.userid
where org.orgid = 'generated-id'
from the documents I understand or think I understand how to do it with one definition using an AS like condition, but even then Im still not sure. Eventually this will branch out into a multi table join, based on cross table conditions so not sure how to achieve this to begin with.
I think this is what are you looking for
$db = Zend_Db::factory( ...options... );
$select = $db->select()
->from(array('org' => 'orgTable'),
array(
'orgid' => 'org.orgid',
'role' =>'org.role',
'userid' =>'user.userid',
'firstname' =>'user.firstname'
))
->join(array('user' => 'userTable'),
'org.userid = user.userid',array())
->where('org.orgid = ?',$generated_id);
Here is a Zend_Db_Select that returns the result you are looking for.
$select = $db->select()
->from(array('org' => 'orgTable'), array('orgid', 'role'))
->join(array('user' => 'userTable'), 'org.userid = user.userid', array('userid', 'firstname'))
->where('org.orgid = ?', 'generated-id');
You can use the array notation for table names to get the aliased names in the query.
Hope that helps.
In zend framework 2 , the following code helps you what are you looking for
$generated_id = 1 ;
$select = new \Zend\Db\Sql\Select( array('org' =>'orgTable'));
$select->columns(array('orgid','role') )
->join( array('user' => 'userTable'),
'org.userid = user.userid',
array('userid','firstname')
)->where( array('org.orgid' => $generated_id ) );
if your adapter platform is mysql, then for printing sql
$mysqlPlatform = new \Zend\Db\Adapter\Platform\Mysql();
echo $select->getSqlString( $mysqlPlatform );
which print sql as
SELECT
`org`.`orgid` AS `orgid`,
`org`.`role` AS `role`,
`user`.`userid` AS `userid`,
`user`.`firstname` AS `firstname`
FROM
`orgTable` AS `org`
INNER JOIN `userTable` AS `user`
ON `org`.`userid` = `user`.`userid`
WHERE
`org`.`orgid` = '1'

Zend_Db_Select: Working with JOIN's

I have this query:
SELECT
groups.name
categories.name,
categories.label
FROM
groups
JOIN
categories
ON
(categories.group1 = groups.id
OR
categories.group2 = groups.id)
AND
groups.label = :section
AND
categories.active = 1
Now, this is my JOIN using Zend_Db_Select but it gives me array error
$select->from($dao, array('groups.name', 'categories.name', 'categories.label'))
->join(array('categories', 'categories.group1 = groups.id OR categories.group2 = groups.id'))
->where('groups.label = ?', $group)
->where('categories.active = 1');
My error:
Exception information:
Message: Select query cannot join with
another table
Does anyone know what I did wrong?
UPDATE / SOLUTION:
I've found the solution thanx to Eran. I just post the solution here in case anyone else is stuck on a problem like this one. The solution is:
$db = Zend_Registry::get('db');
$dao = new Default_Model_Db_CategoryDao('db');
$select = $dao->select();
$select->setIntegrityCheck(false)
->from(array('c' => 'categories'), array('name', 'label'))
->join(array('g' => 'groups'), 'c.group1 = g.id OR c.group2 = g.id', 'g.label')
->where('g.label = ?', $group)
->where('c.active = 1');
return $dao->fetchAll($select);
You are using a Zend_Db_Table_Select object. Those by default have integrity check enabled and cannot select data that is outside of their table.
You can turn it off by adding -> setIntegrityCheck(false) to the select object before querying with it.
You can read more about it in the manual under Select API -> Advanced usage

Lots and lots of joins in a Zend Query, hopefully just a slight tweak

Apologies for all this code, anyhow Im re-working a query into the Zend query way of working, this is what I have so far:
$db = Zend_Registry::get ( "db" );
$stmt = $db->query('
SELECT recipe_pictures.picture_id, recipe_pictures.picture_filename, course.course_name, cuisines.name, recipes.id, recipes.Title, recipes.Method, recipes.author, recipes.SmallDesc, recipes.user_id, recipes.cuisine, recipes.course, recipes.Created_at, recipes.vegetarian, recipes.Acknowledgements, recipes.Time, recipes.Amount, recipes.view_count, recipes.recent_ips, guardian_writers.G_item, guardian_writers.G_type
FROM recipes
LEFT JOIN course ON recipes.course = course.course_id
LEFT JOIN recipe_pictures ON recipes.id = recipe_pictures.recipe_id
LEFT JOIN cuisines ON recipes.cuisine = cuisines.id
LEFT JOIN guardian_writers ON recipes.author = guardian_writers.G_author
WHERE recipes.id = ?', $id);
$stmt->setFetchMode(Zend_Db::FETCH_ASSOC);
$recipes = $stmt->fetchAll();
return $recipes;
That one above works, trying to get the Zend version properly, my effort is below.
$db = Zend_Registry::get ( "db" );
$select = $db->select()
->from(array('r' => 'recipes'))
->join(array('c' => 'course'),
'r.course = c.course_id')
->join(array('rp' => 'recipe_pictures'),
'r.id = rp.recipe_id')
->join(array('cui' => 'cuisines'),
'r.cuisine = cui.id')
->join(array('gw' => 'guardian_writers'),
'r.author = gw.G_author')
->where(' id = ? ', $id);
$recipes = $db->fetchRow($select);
return $recipes;
If anyone can spot an error Id be very grateful, thanks
Use joinLeft instead of join to produce left joins.
To fetch specific columns from a table, rather than all (*) use this:
->from(array('r' => 'recipes'), array('id', 'title', 'method'))
or
->joinLeft(array('rp' => 'recipe_pictures'),
'r.id = rp.recipe_id',
array('picture_id', 'picture_filename')
)
To fetch no columns from a table, pass an empty array as the third parameter.
The join method provides an sql INNER JOIN. If you want to get a LEFT JOIN you should use joinLeft.

Categories