CakePHP Call To A Member Function query() On Null - php

So I am trying to make an ajax call with an entity Id and query a data of that. As far as I know, if an url is such abc.com/xyz/123 then it will hit xyz controller with 123 as parameter and I did that but it says, "An Internal Error Has Occurred". The error log states the headline of this thread.
The method is:
public function fetch($order_details_id)
{
$sql = "SELECT
orders.id
, ROUND(SUM(rolls.weight),2) as produced_qty
FROM orders
LEFT JOIN order_details ON order_details.buyer_order_id = orders.id
LEFT JOIN knitting_plans ON knitting_plans.buyer_order_detail_id = order_details.id
LEFT JOIN jobcards ON jobcards.knitting_plan_id = knitting_plans.id
LEFT JOIN machines ON machines.id = jobcards.machine_id
LEFT JOIN rolls ON rolls.jobcard_id = jobcards.id
WHERE order_details.id = $order_details_id
ORDER BY orders.id";
$data = $this->BuyerOrder->query($sql);
$this->set('data', $data);
}
What's wrong in this code?
URL: /buyerOrders/fetch/52629
Since my objective is to return the result in html/json format. Let me know if I could return in a better way.

load model before query execution
It's trying to call query on null, the only query call here is on $this->BuyerOrder, so it's clear that it unable to find BuyerOrder.
So load model
$this->loadModel('BuyerOrder');

Related

inner join Eloquent as JSON, bad answer

I have problem with join in Eloquent. I have function:
public function getSearchQuery($search_phrase) {
return Car::select("car_models.id", "car_models.name", "brands.name")
->join("car_models", "cars.car_model_id", "=", "car_models.id")
->join("brands", "car_models.brand_id", "=", "brands.id")
->get();
}
This function generate correct request SQL like:
select `car_models`.`name`, `brands`.`name` from `cars` inner join `car_models` on `cars`.`car_model_id` = `car_models`.`id` inner join `brands` on `car_models`.`brand_id` = `brands`.`id` where `cars`.`deleted_at` is null
and it works but I get only brand, without car_model, someone know why ? I return JSON to typeahead.
Regards
Both columns you're selecting are called "name". Try using an alias:
Car::select("car_models.id", "car_models.name as model_name", "brands.name as brand_name")
and you should be fine.

implement joins with laravel 5

I am trying to implement following join query
SELECT * from users as u
LEFT JOIN `table1` as ou
ON ou.user_id = u.id
LEFT JOIN table2 as o
ON o.id = ou.orid
LEFT JOIN table3 as p
ON o.pra_id = p.id;
with my laravel model so i create a function named alldata() in my user model with following code
public function alldata()
{
return $this
->leftjoin('table1','users.id','=','table1.user_id')
->leftjoin('table2','table1.orid','=','table2.id')
->leftjoin('table3','table2.pra_id','=','table3.id');
}
now when i try to access the data by $gd = User::find(1)->getall() it returns results with all the table
but when i try to acces $gd = User::all()->alldata() it gaves error mathod not found
how can i resolve this
thanks
The User::find(1) function returns an single instance of the object (or NULL if not found).
The User::all() function returns a Collection of all User objects.
You need to create a query then get() the results as follows...
public static function alldata()
{
return self::query()
->leftjoin('table1','users.id','=','table1.user_id')
->leftjoin('table2','table1.orid','=','table2.id')
->leftjoin('table3','table2.pra_id','=','table3.id')
->get();
}
Assuming your your alldata() method is in the User class you can call the function with the following code:
User::alldata();
Laravel has a excellent database relationship system which is most defiantly worth looking at. It would make queries like the one above much simpler...
https://laravel.com/docs/5.1/eloquent-relationships

How to select join multiple table without conditional in Codeigniter?

I want to select multiple table using join in codeigniter but I don't want to make any conditional as below code
public function get_invoice(){
$this->db->select('isp.*,dp.*,ip.*');
$this->db->from('isp');
$this->db->join('dp','dp.status = isp.status');
$this->db->join('isp','isp.status = ip.status');
$this->db->limit(5000);
$this->query = $this->db->get();
if($this->query->num_rows()>0){
return $this->query->result();
}
}
however I want to get all data in that table without conditional but in join statement have to used conditional.
So how can I select data from multiple table without conditional in codeigniter?
Thanks for help
Yes you can join query without condition. INNER LEFT RIGHT JOINS need condition.So you can do Cross JOIN.LIKE this
public function get_invoice(){
$this->db->select('isp.*,dp.*,ip.*');
$this->db->from('isp,dp,ip');
$this->db->limit(5000);
$this->query = $this->db->get();
if($this->query->num_rows()>0)
{
return $this->query->result();
}
}
But at Cross JOIN if your isp has 100,dp has 20, and ip has 50 record It will produce (100*20*50) record.I hope you know what CROSS,LEFT,INNER,RIGHT JOIN do.

Running 2 queries in model (codeigniter)?

For example, if I run one query in model:
public function list_users() {
$q = "SELECT user_id, username FROM users";
return $q->result_array();
}
And now, to lists posts from that user, I need to refer to it's id within this function:
public function list_posts() {
$q = "SELECT post_id, post_title, post_content FROM posts
WHERE user_id = what??";
return $q->result_array();
}
OK both of these functions are in Model. Now, How to use RESULT from list_users() in list_posts(). Please have in mind that I need to pass ARRAY of IDs and, to use it only for particular id from list_users() which also returns ARRAY
I know I can use joined query, but that's not the point at all, as I have lots of queries that I need to split
why arent you using a JOIN statement, and making two queries into one?
This will reduce db load, decrease query times, and also reduce clutter in your models.
SELECT p.post_id, p.post_title, p.post_content, u.user_id, u.username FROM posts p LEFT JOIN users u ON u.user_id = p.user_id
you can also do this using active records. Which will avoid having to use full blown queries, and more of a CI methodology to SQL.
http://codeigniter.com/user_guide/database/active_record.html
$this->db->select('p.post_id, p.post_title, p.post_content, u.user_id, u.username');
$this->db->from('posts p');
$this->db->join('users u', 'u.user_id = p.user_id');
$q = $this->db->get();
$q->result();
Edit:
You can return the value as an object.. IE: $this->user_id then reference it in the posts function. Ideally you should call the first function in your Controller, return $user_id and then reference that in your next function.. This is definitely not best case though, you should use JOINs as they are less taxing on the db.
//controller
function test(){
$users = $this->exampleModel->list_users();
//manipulate user data if needed
$posts = $this->exampleModel->list_posts($users);
}

Error while joining tables with Zend_Db

Why am I getting this:
An error occurred
Application error
Exception information:
Message: Select query cannot join with another table
while trying to join two tables?
I have this line code inside my model which extends Zend_Db_Table_Abstract
public function getProjects() {
$select = $this->select()
->from(array('sub' => $this))
->join(array('main' => 'main_projects'), 'main.mai_id = sub.mai_id');
return $this->fetchAll($select);
}
And I use this in my controller: $this->view->entries = $this->sub_projects->getProjects();
Why the hell I get this error? I just want to make a simple join
SELECT sub.*, main.mai_title FROM sub_projects AS sub INNER JOIN main_projects AS main ON sub.mai_id = projects.mai_id;
enter code here
I think here is your solution and explanation : http://www.mail-archive.com/fw-general#lists.zend.com/msg24553.html
Another solution here : Translating a query to use Zend_Db_Select

Categories