I created 2 textension in magento along with 2 different tables. First extension store data in table-1 while second second extension store data in table-2. Now i want to display data in first extension by LeftJoin. It show data without leftjoin from first table but not showing data with leftjoin from both the tables.
This code in block.php
public function methodblock()
{
$collection = Mage::getModel('test/test')->getCollection();
$returnCollection = $collection->getSelect()
->joinLeft('magento_answer', 'id_pfay_test=question_id',
array('*'), null , 'left');
return $returnCollection;
}
On Layout side. dislplaydata.phtml
<?php
$collection = $this->testmethodblock();
foreach($collection as $rows {
echo $rows ->getData('name');
}
I Got the answer. I use the custom query which works for me.
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$qTable = $resource->getTableName('pfay_test');
$aTable = $resource->getTableName('answer/answer');
$query = 'SELECT * FROM '.$qTable.' q left join '.$aTable.' a ON a.question_id=q.id_pfay_test';
$results = $readConnection->fetchAll($query);
return $results;
Related
I am having trouble in creating a query on a search field. There are two tables I have in my database and when I perform my search functionality I need only the gsNumber as a requirement on the search field.
I got stuck on the query since I need to use partial, and I have a problem on what's needed to select on both tables. I don't get on what's the use of partial.
Here is my code below:
public function getDetails($gsNumber,$page = 1, $limit = 5 ){
$em = $this->getEntityManager();
$query = $em->createQuery(
'SELECT partial a.{ediTransactionId,gsNumber},
partial b.{}
FROM MatrixEdiBundle:Edi997Details b
JOIN a.ediTransaction b
WHERE a.errorCodeId != 1 AND a.gsNumber LIKE :gsNumber
AND b.flag = 1')
->setParameter('gsNumber', "%$gsNumber%")
->setFirstResult(($page-1)*$limit)
->setMaxResults($limit);
$paginator = new Paginator($query, $fetchJoinCollection = false );
$paginator->setUseOutputWalkers(false);
return $paginator;
}
i have a database named "products" which has a column "categories". This table contain four category of products namely electronic,Decoration,clothes and vehicle. My target to show these category with their count ie:if there are four products belongs to category electronic, then output should be like this :electronic=4 and so on
My code
public function category()
{
$arrayCategorys = ['electronic','Decoration','clothes','vehicle'];
$data = [];
foreach($arrayCategorys as $arrayCategory)
{
$sql = "SELECT count(id) FROM products WHERE categories='$arrayCategory'";
$records = \DB::select($sql);
$data = array_merge_recursive($data, [
"{$arrayCategory}" =>isset($records[0]->count),
]);
$data=array_filter($data);
dd($data);
}
}
I want show output like this
'electronic'=>'4',
'Decoration'=>'2',
'clothes'=>'2',
'vehicle'=>'1' according to data in database
but iam getting nothing ,[]
You can GROUP BY your categories like this way when you COUNT
SELECT categories,COUNT(*)
FROM products
GROUP BY categories;
For Idea: http://www.w3resource.com/mysql/aggregate-functions-and-grouping/aggregate-functions-and-grouping-count-with-group-by.php
EDIT: Though i am not familiar with laravel5 syntax but this may work for you
$result = DB::table('products')
->select('products.categories',
DB::raw('count(products.id) as category_count')
)
->orderBy('products.id', 'asc')
->groupBy('products.categories')
->get();
You used isset($records[0]->count) but the column name for the count will be count(id). Name the count as count like this "SELECT count(id) AS count FROM products WHERE categories='$arrayCategory'". And you wont be able to get the count just by checking if it is set. Remove the isset and just use $records[0]->count. The code should look like:
public function category()
{
$arrayCategorys = ['electronic','Decoration','clothes','vehicle'];
$data = [];
foreach($arrayCategorys as $arrayCategory)
{
$sql = "SELECT count(id) AS count FROM products WHERE categories='$arrayCategory'";
$records = \DB::select($sql);
$data = array_merge_recursive($data, [
"{$arrayCategory}" =>$records[0]->count,
]);
$data=array_filter($data);
dd($data);
}
}
I am using codeigniter.
I have two function in the model.
In the first function, I get all specific rows from one table. This is where I want to get all product details.
function getProduct()
{
$shoes = 'SELECT pd.name,
pd.id,
pd.price
FROM product_detail pd
LEFT JOIN product_sub_category psc
ON pd.sub_category_id = psc.id
WHERE psc.name = "Shoes"';
$categoried = $this->db->query($shoes);
return $categoried->result();
}
While in the second function, I get all specific rows based on one row from the first function. This is where I want to get all images for each products.
function getImage()
{
$shoes = 'SELECT pim.file_name
FROM product_img pim
LEFT JOIN product_detail pd
ON pim.product_detail_id = pd.id
WHERE pim.product_detail_id = "'.$row->id.'"';
$categoried = $this->db->query($shoes);
return $categoried->result();
}
But it gives me some errors.
Can someone please help me? Thanks for the help.
[UPDATE]
This is my Controller
In the controller, I identify records to get the product, and recorded to get the image.
function index()
{
$this->load->model('site_model');
$data = array();
$query = $this->site_model->getProduct();
$data['records'] = $query;
foreach ($query as $row)
{
$id = $row->id; // get ID
$name = $row->name; // get ID
$product_image = $this->site_model->getImage($id); // invoke the second method feeding that ID
$data['recorded'] = $product_image;
}
$this->load->view('mothershop', $data);
}
This is my Model
function getProduct()
{
$shoes = 'SELECT pd.name,
pd.id,
pd.price
FROM product_detail pd
LEFT JOIN product_sub_category psc
ON pd.sub_category_id = psc.id
WHERE psc.name = "Shoes"';
$categoried = $this->db->query($shoes);
return $categoried->result();
}
function getImage($id)
{
$shoes = 'SELECT pim.file_name
FROM product_img pim
LEFT JOIN product_detail pd
ON pim.product_detail_id = pd.id
WHERE pim.product_detail_id = "'.$id.'"';
$categoried = $this->db->query($shoes);
return $categoried->result();
}
This is my View of mothershop.php
In this section, I call the records and recorded
foreach ($records as $row)
{
echo "<div>".$row->name."</br>";
foreach ($recorded as $rowed)
{
echo "<img src='".base_url()."upload/thumbs/".$rowed->file_name."'/>";
}
echo "</div>";
}
However, I don't know why, it only get the same image for each products.
But if i print the $product_image in the controller, it gives me different images for each products.
What is wrong here?
Thanks in advance for #Ghost
Since that $row->id is solely depending on the first fetching method, currently, its out of scope. In order to use that ID from the first method, just add an argument on the second method and use that to the second query. More like this:
function getImage($id)
Then you can use it on the second method.
Since you haven't shown any code on how you are using your methods. Consider this example:
First method usage (Controller):
$this->load->model('Super_model');
$products = $this->Super_model->getProduct();
foreach($products as &$row) {
$id = $row->id; // get ID
$product_image = $this->Super_model->getImage($id); // invoke the second method feeding that ID
if(!empty($product_image)) { // if it exists
$row->file_name = $product_image->file_name; // add another property which is file_name on that first fetched data
}
}
Model method:
function getImage($id)
{
$shoes = "SELECT pim.file_name
FROM product_img pim
LEFT JOIN product_detail pd
ON pim.product_detail_id = pd.id
WHERE pim.product_detail_id = '$id' ";
$categoried = $this->db->query($shoes);
return ($categoried->num_rows() > 0) ? $categoried->row() : null;
}
That's the basic idea of it, we don't know what goes on and how you glue everything else on the controller, adjust it to fit into your codebase.
I am learning symfony framework. Now I am getting all data from mysql table orders this way:
$c = new Criteria();
$this->items = OrdersPeer::doSelect($c);
Also I have other table orders_complete. How can I get data from tables orders and orders_complete?
I want to do this query:
SELECT * FROM orders, orders_complete WHERE orders.id =
orders_complete.id
If I remember right, with propel you should be able to do something like :
$c = new Criteria();
$orders = OrdersPeer::doSelect($c);
foreach($orders as $order) {
$complete = $order->getOrderCompletes();
// do something with $order and $complete ...
}
This providing that you have defined the two tables relationship within your schema file...
Do check the propel documentation regarding this : http://propelorm.org/documentation/04-relationships.html#using-relationships-in-a-query
So here is my query:
public function fetchAd($adID){
$row = $this->tableGateway->select(function(Select $select) use ($adID){
$select->join('adDetails','adDetails.adID = ads.adID',array('*'),'inner');
$select->where(array('ads.adID' => $adID));
});
return $row->current();
}
So what I'm doing I'm querying the ad table and join the adDetails table in order for me to get the details for a certain AD, the problem is that the entity AD which belongs to the model that I'm doing the query, it doesn't have the columns names(variables) from the adDetails table; so it will only return the columns from the AD table, because the entity doesn't have those fields in the exchangeArray()
I have tried to extend the AD entity to use the AdDetails entity but it returns now to object array but with the fields as null, because it can;t populate them.
So, how should I do this, in order for me to have all the columns available in the model for the tables that will join?
I'm planning to join other tables as well.
Ok I solved the problem, the thing is that it will return an array, and it won't use the ORM style, but it does the job, for now relations are not supported in ZF2 like in ZF1;
use Zend\Db\Sql\Sql,
Zend\Db\Sql\Where;
$sql = new Sql($this->tableGateway->getAdapter());
$select = $sql->select();
$select->from($this->tableGateway->table)
->join('adDetails','adDetails.adID = ads.adID',array('*'),'inner');
$where = new Where();
$where->equalTo('ads.adID', $adID) ;
$select->where($where);
$statement = $sql->prepareStatementForSqlObject($select);
$result = $statement->execute();
return $result->current();
public function fetchJoin()
{
$select = new \Zend\Db\Sql\Select;
$select->from('tablea a');
$select->columns(array('*'));
$select->join('tableb b', "b.id = a.b_id", array('field1'), 'left');
// to display query string remove comment in next line
//echo $select->getSqlString();
$resultSet = $this->tableGateway->selectWith($select);
return $resultSet;
}