count number of foreign key in doctrine - doctrine

This is follow up of this question: count number of foreign keys
I am using doctrine 1.2 and I want to count the number of business that belongs to subcategory.
Following are the mysql tables.
1.fi_category
+----+-----------------+-----------------+
| id | name | slug |
+----+-----------------+-----------------+
2.fi_subcategory
+----+-----------------+-----------------+-------------+
| id | name | slug | category_id |
+----+-----------------+-----------------+-------------+
3.fi_business_subcategory
+----+-------------+----------------+
| id | business_id | subcategory_id |
+----+-------------+----------------+
I am using this DQL.
$q = Doctrine_Query::create()
->select('c.name, c.slug, sc.name, sc.slug')
->from('Model_Category c')
->leftJoin('c.Subcategory sc')
->leftJoin('sc.BusinessSubcategory bsc');
which gives me something like this.
Array
(
[0] => Array
(
[id] => 1
[name] => Entertainment & Lifestyle
[slug] => entertainment-lifestyle
[Subcategory] => Array
(
[0] => Array
(
[id] => 1
[name] => Arts and Crafts
[slug] => arts-and-crafts
)
[1] => Array
(
[id] => 2
[name] => Family
[slug] => family
)
[2] => Array
(
[id] => 3
[name] => Fashion
[slug] => fashion
)
)
)
)
I am looking to fetch the number of business, i.e the returned result should be something like this depending on the business it belongs.
Array
(
[0] => Array
(
[id] => 1
[name] => Entertainment & Lifestyle
[slug] => entertainment-lifestyle
[Subcategory] => Array
(
[0] => Array
(
[id] => 1
[name] => Arts and Crafts
[slug] => arts-and-crafts
[business_count] => 35
)
[1] => Array
(
[id] => 2
[name] => Family
[slug] => family
[business_count] => 10
)
[2] => Array
(
[id] => 3
[name] => Fashion
[slug] => fashion
[business_count] => 27
)
)
)
)
Tried various ways using DQL, but nothing seems to work out. Any idea how should I go with what I want?

Did you try:
$q = Doctrine_Query::create()
->select('c.name, c.slug, sc.name, sc.slug, COUNT(sc.BusinessSubcategory.id) AS business_count')
->from('Model_Category c')
->leftJoin('c.Subcategory sc')
->groupBy('c.id, sc.id');
with HYDRATE_ARRAY

Related

Laravel Trying to Get Property of Non-Object - Display Data from Different Table using API

I have two table:
1) ip status table
2) status table
In the database, status's id is the foreign key for ip status table. I want to display 'whitelist' instead of '2'
ip status table:
+==============+===============+============+
| ip_status_id | ip_address | status_id |
+==============+===============+============+
| 1 | 192.108.1.1 | 2 |
+--------------+---------------+------------+
status table:
+============+================+
| status_id | status |
+============+================+
| 1 | Blacklist |
+------------+----------------+
| 2 | Whitelist |
+------------+----------------+
and I call it through API with query like this:
$stmt = $db->prepare("SELECT * FROM tbl_ip_statuses left join
tbl_status on tbl_ip_statuses.status_id = tbl_status.status_id
WHERE tbl_ip_statuses.is_active = ?
AND tbl_ip_statuses.is_deleted = ?
group by tbl_ip_statuses.ip_status_id");
$stmt->bind_param("ii", $_GET["is_active"], $_GET["is_deleted"]);
And I do checking at controller, the data fetch exactly like I want
Here is my controller:
$listing = json_decode($ip_status);
//dd($listing);
return view('ip_status.listing')->with("ip_status", $listing);
Here is my view:
#if($ip_status)
#foreach($ip_status as $data)
<td class="uk-width-1-2">{{ $data->s_ip }}</td>
<td class="uk-width-1-2">{{ $data->s_status }}</td>
But then it show trying to get property s_ip of non object.
when I print_r($listing)
stdClass Object (
[ip_statuses] => Array (
[0] => stdClass Object (
[ip_status_id] => 1
[s_ip] => 192.108.1.1
[status_id] => 2
[is_active] => 1
[ip_status_id_create] => 0
[dt_create] => 2020-04-15 17:16:06
[ip_status_id_edit] => 0
[dt_edit] =>
[is_deleted] => 0
[ip_status_id_delete] => 0
[dt_delete] =>
[s_status] => Whitelist
[status_id_create] => 0
[status_id_edit] => 0
[status_id_delete] => 0
)
[1] => stdClass Object (
[ip_status_id] => 2
[s_ip] => 1922.131.100.1
[status_id] => 1
[is_active] => 1
[ip_status_id_create] => 0
[dt_create] => 2020-04-15 17:15:29
[ip_status_id_edit] => 0
[dt_edit] =>
[is_deleted] => 0
[ip_status_id_delete] => 0
[dt_delete] =>
[s_status] => Blacklist
[status_id_create] => 0
[status_id_edit] => 0
[status_id_delete] => 0
)
[2] => stdClass Object (
[ip_status_id] => 3
[s_ip] => 1.1.1.1
[status_id] => 2
[is_active] => 1
[ip_status_id_create] => 0
[dt_create] => 2020-04-15 17:16:06
[ip_status_id_edit] => 0
[dt_edit] =>
[is_deleted] => 0
[ip_status_id_delete] => 0
[dt_delete] =>
[s_status] => Whitelist
[status_id_create] => 0
[status_id_edit] => 0
[status_id_delete] => 0
)
)
)
ip_statuses in an array as I see from dd result,
You can access s_ip an other elements like this $listing[‘s_ip’]
Update:
Your data is reaching blade as one ‘ip_statuses’ which have array items inside, so you need to make a change in controller where you send data to view:
return view(...)->with(‘ip_status’, $listing->ip_statuses);

Echo from foreach

i have model that return
return array(
'senin'=> $senin->result(),
'selasa'=> $selasa->result(),
'rabu'=> $rabu->result()
);
and controller
$data['ot'] = $this->genbamodel->getOt();
in my view, i use 'foreach $ot as key' and when i tried to print_r($key) the result like this
Array()
Array ( [0] => stdClass Object ( [id_genba] => 1 [hari] => Tuesday [shift1] => 2 [shift2] => 0 ))
Array ( [0] => stdClass Object ( [id_genba] => 1 [hari] => Wednesday [shift1] => 1 [shift2] => 2 ))
how to get just shift1 = 2 and shift2 = 0 (from second array / hari=>Tuesday ??
Thankyou
I don't think you need the foreach loop to access those values. To get just the second record, you can do the below.
$ot['selasa'][0]->shift1
$ot['selasa'][0]->shift2
$ot['selasa'][0]->hari

How can I sort an array by specific value containing class object inside it?

I want to sort this array by product price in ascending order. How can I do this?
Array
(
[0] => stdClass Object
(
[product_id] => 16
[product_name] => Ferrari 4802
[product_details] => I know this car is good. Because I have driven this car 10 year.
[product_quantity] => 1
[product_price] => 2560
[shipping_cost] => 1000
[product_image] => ./admin_assets/image/uploads/product_image/ea28444f93d1fea413c95861bf306fbf.jpg
[product_details_image] => ./admin_assets/image/uploads/product_details/ea28444f93d1fea413c95861bf306fbf.jpg
[thumbnails_image] => ./admin_assets/image/uploads/thumbnails/ea28444f93d1fea413c95861bf306fbf.jpg
[slidder_image] => ./admin_assets/image/uploads/slidders/ea28444f93d1fea413c95861bf306fbf.jpg
[status] => 1
[slidder] =>
[category_id] => 4
)
[1] => stdClass Object
(
[product_id] => 15
[product_name] => Laborghini urus
[product_details] => I know this car is good. Because I have driven this car 10 year.
[product_quantity] => 2
[product_price] => 500
[shipping_cost] => 1000
[product_image] => ./admin_assets/image/uploads/product_image/68a7eb1c6bf5b3b43f0c3fd4ed143c68.jpg
[product_details_image] => ./admin_assets/image/uploads/product_details/68a7eb1c6bf5b3b43f0c3fd4ed143c68.jpg
[thumbnails_image] => ./admin_assets/image/uploads/thumbnails/68a7eb1c6bf5b3b43f0c3fd4ed143c68.jpg
[slidder_image] => ./admin_assets/image/uploads/slidders/68a7eb1c6bf5b3b43f0c3fd4ed143c68.jpg
[status] => 1
[slidder] =>
[category_id] => 4
)
[2] => stdClass Object
(
[product_id] => 14
[product_name] => Laborghini hurican023
[product_details] => I know this car is good. Because I have driven this car 10 year.
[product_quantity] => 4
[product_price] => 50
[shipping_cost] => 1000
[product_image] => ./admin_assets/image/uploads/product_image/65e660ef6e3ebcb0773d87c048c93e1b.png
[product_details_image] => ./admin_assets/image/uploads/product_details/65e660ef6e3ebcb0773d87c048c93e1b.png
[thumbnails_image] => ./admin_assets/image/uploads/thumbnails/65e660ef6e3ebcb0773d87c048c93e1b.png
[slidder_image] => ./admin_assets/image/uploads/slidders/65e660ef6e3ebcb0773d87c048c93e1b.png
[status] => 1
[slidder] =>
[category_id] => 4
)
)
Because the array is generated by a database query you can easily have the query do the sorting for you.
You don't show your model code but assuming you are using Query Builder (a.k.a. Active Record) you need the following in your model before you execute $this->db->get().
$this->db->order_by('product_price', 'ASC');
Here is some example code showing a full query. It assumes the table is named 'products', you want to select all the table's fields, and you want only a particular 'category_id'.
In the Model file:
public function product_by_category($category)
{
$query = $this->db
->where('category_id', $category)
->order_by('product_price', 'ASC')
->get('products');
//return all records or NULL if no records are found
return $query->num_rows() > 0 ? $query->result() : NULL;
}

Get the ordered product size and color in order items admin grid in Magento 1.9.2

I am showing a list of items ordered from all the orders from 'sales_flat_order_item table' in Magneto 1.9.2. I get the name, SKU, and all other values from the following collection.
protected function _prepareCollection() {
$collection = Mage::getModel('sales/order_item')->getCollection();
$collection->getSelect()->join(array('o' => 'sales_flat_order'), 'main_table.order_id = o.entity_id', array('increment_id','created_at'));
$this->setCollection($collection);
parent::_prepareCollection();
return $this;
}
Now I want to get the size and color of the item ordered. I am new to Magento, so I have no clue where I can get these values from.
You can access them in sales_flat_order_item table collection.
$data = $collection->getData();
$optionsData = unserialize($data['product_options']);
echo "<pre>";print_r($optionsData);
// check the [attributes_info] for the custom options
The data in product_options is serialized, so if you unserialize it you end up with something like below :
Array
(
[info_buyRequest] => Array
(
[uenc] => aHR0cDovLzUwLjU2LjIxMy4xNTcvbWVuL3RlZXMta25pdHMtYW5kLXBvbG9zL2NoZWxzZWEtdGVlLTUxNC5odG1sP29wdGlvbnM9Y2FydA,,
[product] => 409
[related_product] =>
[super_attribute] => Array
(
[92] => 20
[180] => 80
)
[qty] => 6
[return_url] =>
)
[attributes_info] => Array
(
[0] => Array
(
[label] => Color
[value] => Black
)
[1] => Array
(
[label] => Size
[value] => S
)
)
[simple_name] => Chelsea Tee
[simple_sku] => mtk004
[product_calculations] => 1
[shipment_type] => 0
[giftcard_lifetime] =>
[giftcard_is_redeemable] => 0
[giftcard_email_template] =>
[giftcard_type] =>
)

Storing booking items information in session array of codeigniter

I wanted to store booking items such as
--> pid =1, qty=3
--> pid=5, qty=4
and so on
in codeigniter session.
I am finding it difficult and i am running out of deadline. Helping hands are very much appreciated.
I have made an example special for you. Suppouse it will helps
$data = array();
for ($i = 1; $i<=5; $i++)
{
$data[] = array('pid' => $i, 'qty' => $i);
}
$this->session->set_userdata('order_data', $data);
and output your result will be shown in session like these :
[order_data] => Array
(
[0] => Array
(
[pid] => 1
[qty] => 1
)
[1] => Array
(
[pid] => 2
[qty] => 2
)
[2] => Array
(
[pid] => 3
[qty] => 3
)
[3] => Array
(
[pid] => 4
[qty] => 4
)
[4] => Array
(
[pid] => 5
[qty] => 5
)
)
If you want to call only these data (booking data) then call
$this->session->userdata['order_data']
you can free use it in foreach for view like
foreach ($this->session->userdata['order_data'] as $key => $value)
{
}

Resources