Given a query :
SELECT t1.c1, t2.c1 FROM t1,t2;
How to write it in Zend framework without using joins ?
Try the below join query in zend framework,
$sql = $this->tableGateway->getSql()->select();
$sql->join('table2', "table1.column1 = table2.column1", array(column1,column2), 'inner');
Assuming you have a common field (we'll call it c2), you can do something approximating a JOIN without actually doing a JOIN.
// we'll assume $adapter is an instance of Zend_Db_Adapter_Mysqli
$cols = $adapter->fetchAll(
'
SELECT
t1.c1 AS t1c1,
t2.c1 AS t2c1
FROM t1, t2
WHERE t1.c2 = t2.c2
'
);
Using Zend_Db_Select you can do it this way
$rows = $adapter->select()
->from('t1', array('t1c1' => 'c1'))
->from('t2', array('t2c1' => 'c1'))
->where('t1.c2 = t2.c2')
->query()->fetchAll();
Related
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);
I want to do this:
SELECT * FROM `mydb`.`table1`
INNER JOIN `mydb`.`table2`
ON `table2`.`table1_id` = `table1`.`id`
INNER JOIN `mydb`.`table3`
ON `table3`.`id` = `table1`.`table3_id`
WHERE `table2`.`myfield2`='string1'
AND `table3`.`myfield3` = 'string2';
query on Propel, but can't find the right way to do it. With only two tables join() seems to work fine like this
$query = Table1Query::create()
->join('Table2')
->where('Table2.myfield2 = ?', $string1)
->filterByMyfield3($string2)
->findOne();
But how does it work with three or more tables?
Of course it is possible. You have described the mapping in a config file.
Example :
$review = ReviewQuery::create()
->joinWith('Review.Book')
->joinWith('Book.Author')
->joinWith('Book.Publisher')
->findOne();
$book = $review->getBook()
$author = $book->getAuthor();
$publisher = $book->getPublisher();
http://propelorm.org/documentation/04-relationships.html
I have a SQL statement
select a.username
from friendships f1
inner join friendships f2 on (f1.user = f2.friend and f1.friend = f2.user)
inner join users a ON (a.user_id = f1.friend) where f1.user = '24'
I excuted this query on phpmyadmin and it's OK.
But I don't knoe how to do this query in Zend
$select = $this->_db_table->select()
->setIntegrityCheck(false)
->from(array('f1' => 'friendships'), array('u.*'))
->joinInner(array('f2' => 'friendships'),
array('f1.user = f2.friend', 'f1.friend = f2.user'))
->joinInner(array('u' => 'users'), array('u.user_id = f1.friend'))
->where('f1.user = ?', $user_id);
I use this code but it doesn't work, any ideas?
What version of ZF are you using ? Here is how you would do it in ZF 1.12:
$this->_db_table->getAdapter()->select()
->from("friendships as f1")
->joinInner("friendships as f2", "f1.user = f2.friend AND f1.friend = f2.user")
->joinInner("users as a", "a.user_id = f1.friend", array("a.username"))
->where("f1.user = ?", $user_id)
->query()
->fetchAll(); // use ->fetch() if you only want to retrieve one record
Read more on in the manual:
What exactly doesn't work? Are you getting a PHP/Zend error or a SQL error?
You can use $select->assemble() to examine the prepared query and compare it against your expectation:
echo $select->assemble();
I'm using Zend DB to generate a query using the following code:
$table->select()
->setIntegrityCheck(false) //required for multi-table join
->from('modules')
->joinInner(
'basket_modules',
'modules.id = basket_modules.id')
->joinInner(
'baskets',
'baskets.id = basket_modules.basket_id')
->where('baskets.id = ?', $this->id);
This generates the SQL:
SELECT modules.*, basket_modules.*, baskets.*
FROM modules
INNER JOIN basket_modules ON modules.id = basket_modules.id
INNER JOIN baskets ON baskets.id = basket_modules.basket_id
WHERE (baskets.id = '3')
My problem here is with the SELECT part, it's selecting all 3 tables instead of just modules, which is the one I want. So the query I would want to generate is:
SELECT `modules`.*
FROM `modules`
#etc...
How can I do this? If I edit the query manually and run it, it returns what I want so there shouldn't be a problem with the syntax.
Please look at the example in the manual Zend_Db_Select. Scroll to the Example #13.
To select no columns from a table, use an empty array for the list of
columns. This usage works in the from() method too, but typically you
want some columns from the primary table in your queries, whereas you
might want no columns from a joined table.
$select = $db->select()
->from(array('p' => 'products'),
array('product_id', 'product_name'))
->join(array('l' => 'line_items'),
'p.product_id = l.product_id',
array() ); // empty list of columns
you can specify column name for other table and main table like below
$table->select()
->setIntegrityCheck(false) //required for multi-table join
->from('modules',array('modules.*'))
->joinInner(
'basket_modules',
'modules.id = basket_modules.id',array('basket_modules.id'))
->joinInner(
'baskets',
'baskets.id = basket_modules.basket_id',array('baskets.id'))
->where('baskets.id = ?', $this->id);
so sql will be like
SELECT modules.*, basket_modules.id, baskets.id
FROM modules
INNER JOIN basket_modules ON modules.id = basket_modules.id
INNER JOIN baskets ON baskets.id = basket_modules.basket_id
WHERE (baskets.id = '3')
$table->select()
->setIntegrityCheck(false) //required for multi-table join
->from('modules')
->joinInner(
'basket_modules',
'modules.id = basket_modules.id',array(''))
->joinInner(
'baskets',
'baskets.id = basket_modules.basket_id',array(''))
->where('baskets.id = ?', $this->id);
Give a empty array as the third parameter of join otherwise it will select all the field from the table joined.If you want some fields then specify field names in the array while joining.
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.