CakePHP retrieve data from another Model - php

I am new to cakephp and I have a little problem about retrieving data from another model.
My table association is like this:
Items hasMany Stocks
Stocks belongsTo Items
Items belongsTo UnitMeasurement
UnitMeasurement hasMany Items
My problem is I want to show UnitMeasurement Name to Stocks index html table view.
Current Html table view:
----------------------------------------------------------------------
Item | Stock Balance| Unit | Created |
----------------------------------------------------------------------
Microprocessor | 12 | DISPLAY UNIT NAME HERE!| 19/1/2014 |
Microcontroller| 20 | DISPLAY UNIT NAME HERE!| 19/1/2014 |
CPU | 12 | DISPLAY UNIT NAME HERE!| 19/1/2014 |
----------------------------------------------------------------------
How do I make the succesful query ?
Thanks.
EDIT:
This is my find query in my StocksController index function:
public function index() {
$this->Stock->recursive = 0;
$order = "Stock.id DESC";
//set orderby
$stock = $this->Stock->find('all',array('order'=>$order));
$this->set('stocks', $stock);
//query to find UnitMeasurement Name for Stocks Item
foreach ($stock as $stocks) {
//debug($stocks);
$this->loadModel('Items');
$unitMeasurement = $this->Items->find('first',array('fields'=>'unit_measurement_id','conditions'=>array('Items.id'=>$stocks['Stock']['item_id'])));
$this->set('units',$unitMeasurement);
}
}
Thanks again.

check this model name always singular please check and let me know if it works !
foreach ($stock as $stocks) {
//debug($stocks);
//$this->loadModel('Items');
$this->loadModel('Item');
$unitMeasurement = $this->Item->find('first',array('fields'=>'unit_measurement_id','conditions'=>array('Item.id'=>$stocks['Stock']['item_id'])));
$this->set('units',$unitMeasurement);
}

You could make a query like this:
$items = $this->Item->find('all', array(
'fields' => array(
'Item.name',
'SUM(Stock.amount) as amount',
'UnitMeasurement.name',
'Item.created',
),
'joins' => array(
array(
'table' => "stocks",
'alias' => "Stock",
'conditions' => "Item.id = Stock.item_id"
),
array(
'table' => 'unit_measurements',
'alias' => 'UnitMeasurement',
'conditions' => 'UnitMeasurement.id = Item.unit_measurement_id',
),
),
'group' => array('Item.id', 'Item.unit_measurement_id'),
));

Related

Cakephp 4: FIND_IN_SET not returning result

I have an array, $submenus, in my app that I implode to a delimited string:
$subs = implode(',', $submenus);
The string will look something like this: 'ml_,nc_,msr_'. These values are stored in a field called group_prefix in my submenus table. Each submenu row has a unique group_prefix.
The following code builds menus and submenus from a database:
$menus = $this->Menus->find('all', [
'order' => ['Menus.display_order ASC'],
'conditions' => $conditions,
'contain' => [
'Submenus' => [
'conditions' => [
'Submenus.status' => 1,
'FIND_IN_SET("' . $subs . '", Submenus.group_prefix)'
],
]
]
]);
$this->set('menus', $menus);
It works fine until I add the FIND_IN_SET condition on Submenus. When I do, I get no submenus returned, just the main menus. Debug confirms that the string is formatted propery. Doesn't error out, I just get no resultset.
When I run the submenus query in MySQL, it works.
set #prefixes = 'ml_,nc_,msr_';
SELECT `id`, `name` FROM `submenus` WHERE `status` = 1 AND FIND_IN_SET(`submenus`.`group_prefix`, #prefixes);
+----+---------------------------+
| id | name |
+----+---------------------------+
| 4 | Mission Lessons Module |
| 5 | MSR Module |
| 8 | Work Authorization Module |
+----+---------------------------+
What am I missing?
Answer was to reverse the order of arguments in FIND_IN_SET.

Insert records to either two tables or one table depending on if a record exists or not using Laravel query builder

I'm trying to insert records to either two tables or one table depending on if a record exists or not.
First table Authors
ID | Name
1 | Joe
2 | Sam
Second table Books
ID | author_ID | Book
1 | 2 | Book1
2 | 2 | BookYYY
3 | 1 | BookABC
What I want to accomplish is to check if author exists first, if not insert author and his book and if it DOES exists insert just the book with the right author ID
Here is what I've attempted so far that doesn't seem to work.
$result = DB::table('authors')
->where('name', $data['author_name'])
->where('username', $data['author_username'])->pluck('id');
if(is_null($result)){
//Not in table add new author
$id = DB::table('authors')->insertGetId(
['name' => $data['author_name'], 'username' => $data['author_username']]
);
//Add book
DB::table('books')->insert(
['author_id' => '.$id.', 'name' => "Book777"]
);
}
else{
//Is in table insert just book
DB::table('books')->insert(
['author_id' => '.$result.', 'name' => "Book777"]
);
}
So I'm trying to add author with Book name "Book777" but if author does exists in DB get the author ID and insert just the book.
Thank you all for helping me with this! Appreciate any help.
Consider using ORM. With Eloquent you can change all your code to just this:
$author = Author::firstOrCreate(['name' => $data['author_name'], 'username' => $data['author_username']]);
$author->books()->create(['name' => 'Book777']);
With Query Builder you can do this:
$attributes = [
'name' => $data['author_name'],
'username' => $data['author_username']
];
$author = DB::table('authors')->where($attributes)->first();
$authorId = is_null($author) ? DB::table('authors')->insertGetId($attributes) : $author->id;
DB::table('books')->insert(['author_id' => $authorId, 'name' => "Book777"]);
I'm not sure if it's work or not but hope this helps
$result = DB::table('authors')
->where('name', $data['author_name'])
->where('username', $data['author_username'])->pluck('id');
if(!empty($result)){
//Is in table insert just book
DB::table('books')->insert(
['author_id' => $result, 'name' => "Book777"]
);
}
else{
//Not in table add new author
$id = DB::table('authors')->insertGetId(
['name' => $data['author_name'], 'username' => $data['author_username']]
);
//Add book
DB::table('books')->insert(
['author_id' => $id, 'name' => "Book777"]
);
}

how to prevent duplicate child record to database for each parent using codeigniter?

i am having product variant table in mysql, i want to prevent duplicate child record for each parent product id:
-----------------------------------------------------
id | product id | category id | variant_value_id' | title
----------------------------------------------------
1 | 11 | 2 | 7
2 | 11 | 3 | 7
this is my mysql table structure.
i want to have unique variant id for each category id.
this is my controller
foreach($this->input->post('product_variant') as $value){
$variant_data = array(
'product_id' => $id,
'category_id' => $this->input->post('product_category'),
'variant_group_id' => $this->Product_model->get_variant_group_by_variant_id($value)[0]->group_id,
'variant_value_id' => $value,
'product_variant_title' => $this->input->post('product_name').' '.$this->Product_model->get_variant_group_by_variant_id($value)[0]->value,
'mrp_price' => '',
'price' =>'',
'slug' => url_title($this->input->post('product_name').'-'.$this->Product_model->get_variant_group_by_variant_id($value)[0]->value, 'dash', true),
'status' =>'',
);
if($this->Product_model->add_product_variant($variant_data)){
$this->session->set_flashdata('product_variant_added', 'Product Variant Created Succesfully');
}
}
Please help. if need more info, i will provide
If you want to prevent duplicate productId, then use the following I mean do a check:
$q = $this->db->select('ProductName')
->from('Table')
->where(array('ProductId' => $ProductId, 'variant_value_id' => $variant_value_id))->get(); //Select query to check the productId
if($q->num_rows() == 0){ //Finally checks if the Id doesn't exist
//Insert goes here
}
else
{
//Already exists
}

Merge meta table with main table

I have two tables, a main one, and one that supports the main table, very very similar to what wordpress has, posts and posts_meta.
Main table:
id
title,
content
id | title | content
1 | one | content one
2 | two | content two
Meta table:
id
item_id
key
value
id | item_id | key | value
1 | 1 | template | single
2 | 1 | group | top
1 | 2 | template | page
2 | 2 | group | bottom
And my goal is, in the end, have an array with the data from the main table, merged with the meta table. example:
$data = array(
array(
'id' => 1,
'title' => 'one',
'content' => 'content one',
'template' => 'single',
'group' => 'top'
),
array(
'id' => 2,
'title' => 'two',
'content' => 'content two',
'template' => 'page',
'group' => 'bottom'
)
);
What is the best way to achieve this in a way that preforms good?
I am using PDO to connect to my database, and how Im doing right now is, I first query the data on the first table, and then for each result, i query the meta table, I use prepared statements for this, since it's suposed to be fast, but even so, it's harming the performance of my script.
Thank you
Instead of querying meta table for each result from first query
you should extract the ids from the first result:
$rows = q('SELECT * FROM posts');
$byIds = [];
foreach ($rows as &$row)
{
$byIds[$row['id']] =& $row;
}
and run second query:
$rows2 = q('SELECT * FROM posts_meta WHERE item_id IN (' . implode(',', array_keys($byIds)) . ')');
Then loop the results in PHP and merge with first query results.
foreach ($rows2 as $row2)
{
$byIds[$row2['item_id']][$row2['key']] = $row2['value'];
}
You have your merged results in $rows variable now:
var_dump($rows);
This way you will have only 2 db requests.
Please note that i have used $byIds as array of references so i dont have to search row with specific id in second loop. This way order of elements in $rows are preserved.

Sorting and storing array returned by a method as parent and child data for displaying nested/multi level comments in codeigniter 2.0

Hey guys I'm trying to learn codeigniter, but once again I'm STUCK and I seek help (as usual :P )
What I need to do?
-> I need to get the data related to a article from the database along with other stuff like the tags for the article and all the comments. I'm thinking of keeping single level nested comments for the article.
Well I'm done with the tag part [link to the answer which helped me with the same : Returning and using multidimensional array of records from database in CodeIgniter 2.0 ] but the comment part is driving me nuts.
Well to get started here is my comments table
Comments
+---------------+-------------+
| Field | Type |
+---------------+-------------+
| commentId | int(10) |
| PostId | int(10) |
| author | varchar(30) |
| email | varchar(30) |
| url | varchar(50) |
| date | datetime |
| comment | text |
| parent | int(10) |
+---------------+-------------+
I'm using the parent field to keep a track of the parent for a nested child comment. By default the value is 0 which means it the parent. Child comment will have the commentid of its parent comment
public function getPost($postName = NULL , $year = NULL, $month = NULL ){
if($postName != NULL && $year != NULL && $month != NULL){
//single post
$this->load->model('comment_model');
$this->db->where('postName',$postName);
$this->db->where('year(date)',$year);
$this->db->where('month(date)',$month);
$q = $this->db->get('mstack_Post');
if($q->num_rows()>0){
$post = $q->result();
foreach ($post as &$p) {
$p->tags = $this->getAllTags($p->postId);
/* getting the comments */
$com = $this->comment_model->getComments($p->postId);
/*echo count($com).' is the total count'; output= 4 */
foreach ($com as &$c) {
/* trying to filter the comment. but all I get is 1 comment as the output*/
if($c->parent==0){
$p->comments->parentComment = $c;
}elseif($c->commentId==$c->parent){
$p->comments->childComment = $c;
}
}
}
return $post;
}else{
return array();
}
}
}
Any help will surely be appreciated.
If you have any other technique /idea to display multi level comments then do let me know. :)
Here is the solution that might be helpfull:
First you need 2 helper recursive function:
// Building comments.
function buildComments($list, $parent = 0)
{
// Creating result array.
$result = array();
//looping...
foreach ($list as $item)
{
//iteration starts with 0 as default.
if ($item->parent == $parent)
{
// add to the result
$result[$item->commentId] = array(
'author' => $item->author,
// ... other definitions
'child' => buildComments($list, $item->commentId) //execute this function for child.
);
}
}
return $result;
}
function printComments($arg, $depth = 1)
{
foreach ($arg as $item)
{
// Printing comment...
echo str_repeat(' ', $depth) . $item['author'] . "<br />\r\n";
// extra echoes...
// if it has a child comment...
if (count($item['child'] > 0))
{
printComments($item['child'], $depth + 1);
}
}
}
A little explaining:
The buildComments() function will starts with rows that parents has 0. Then it will execute itself for child. if child as a child, it will add it. In the end, result will be like this:
$result = array(
1 => array(
'author' => 'John',
'child' => array(
8 => array(
'author' => 'Jane',
'child' => array(
3 => array(
'author' => 'Jamie',
'child => array()
)
)
),
6 => array(
'author' => 'Jackie',
'child => array()
),
9 => array(
'author' => 'Harry',
'child => array()
)
)
),
4 => array(
'author' => 'Jack',
'child' => array()
),
10 => array(
'author' => 'Clark',
'child' => array(
11 => array(
'author => 'Lois',
'child' => array()
)
)
),
12 => array(
'author' => 'Luthor',
'child' => array()
)
);
In the printComments() function we are printing results recursive. for each child, function repeats itself. You will get result like this:
John
Jane
Jamie
Jackie
Harry
Jack
Clark
Lois
Luthor
For more information about recursive functions Look this answer
USAGE
$this->db->where('postName',$postName);
$this->db->where('year(date)',$year);
$this->db->where('month(date)',$month);
$this->db->order_by('parent', 'asc');
$query = $this->db->get('comments');
$comments = buildComments($query->result());
printComments($comments);
that'is that simple...

Categories