First I'm really new to fuelphp, you can down vote the question if needed.
My problem is that i made a facebook similar wall, and i don't really understand the comments logic.
So i tried to join my tables this way
static function get_stream()
{
$query = DB::select()->from('stream_post');
$query->join('users_metadata');
$query->on('stream_post.user_id', '=', 'users_metadata.user_id');
$query->join('stream_comment');
$query->on('stream_post.stream_id', '=', 'stream_comment.stream_id');
$query->order_by('stream_post.stream_id', 'DESC');
$result = $query->execute();
if(count($result) > 0) {
foreach($result as $row)
{
$data[] = $row;
}
return $data;
}
}
the problem with this is that, this only shows the stream posts what have comments, and doesn't show the others.
So can please someone give me a logic how to join the tables to show those post to what doesn't have a comment?
Try that:
static function get_stream()
{
$query = DB::select()->from('stream_post');
$query->join('users_metadata');
$query->on('stream_post.user_id', '=', 'users_metadata.user_id');
$query->join('stream_comment', 'RIGHT'); // The RIGHT JOIN keyword returns all rows from the right table, even if there are no matches in the left table.
$query->on('stream_post.user_id', '=', 'stream_comment.user_id');
$query->order_by('stream_post.stream_id', 'DESC');
$result = $query->execute();
if(count($result) > 0) {
foreach($result as $row)
{
$data[] = $row;
}
return $data;
}
}
Edit:
That query should work (when every user_id from stream_post has the same user_id in users_metadata. Just transfer this query to fuelphp (I didn't use it before).
SELECT *
FROM stream_post
RIGHT JOIN stream_comment
ON stream_post.stream_id = stream_comment.stream_id
JOIN users_metadata
ON stream_post.user_id = users_metadata.user_id
ORDER BY stream_post.stream_id DESC
Related
code:
public function draft_post($idd)
{
$this->db->select('*');
$this->db->from('registration');
$this->db->join('draft_registration', 'registration.user_id= draft_registration.user_id','INNER');
$this->db->where('registration.user_id', $idd);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
In this codes, I have two table i.e. registration and draft_registration. Now, What am I doing here I want to run inner join in Codeigniter. Now, What happening when I hit this query on phpmyadmin it shows wrong data i.e. I have two rows in draft_registration and one row in registration table but it always shows two table which is wrong and my query looks like when I was print as mention below:
SELECT *
FROM `registration`
INNER JOIN `draft_registration` ON `registration`.`user_id`= `draft_registration`.`user_id`
WHERE `registration`.`user_id` = '20181121064044'
So, How can I resolve this issue? Please help me.
Thank You
$this->db->select('*'); //This code get all rows from both table.If you want a particular row you mention the column name.
For example:
$this->db->select('registration.name,draft_registration.city,draft_registration.state');
Specify column that you want to select. Or if you want select all column of your table, you can use :
SELECT registration.* with backticks `` on column name
Use the Below Code
public function draft_post($idd)
{
$this->db->select('registration.*,draft_registration.*');
$this->db->from('registration');
$this->db->join('draft_registration', 'registration.user_id= draft_registration.user_id');
$this->db->where('registration.user_id', $idd);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
Or you can use with objects
public function draft_post($idd)
{
$this->db->select('a.*,b.*');
$this->db->from('registration a');
$this->db->join('draft_registration b', 'a.user_id= b.user_id');
$this->db->where('a.user_id', $idd);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
Here is my codes:
public function getallcontractfiles()
{
$this->db->select('cd.*, GROUP_CONCAT(cf.Contract_File_Name) AS fileslink');
$this->db->from('contract_details as cd');
$this->db->join('contract_files as cf', 'cd.Contract_Id = cf.Contract_Id','LEFT');
if($this->session->userdata['user_type'] == 'ADMIN' && $this->session->userdata['user_group'] == '' ){
$this->db->where('cd.Company_id',$this->session->userdata['company_id']);
}
if($this->session->userdata['user_type'] == 'USER'){
$this->db->where('cd.users_id',$this->session->userdata['logged_user']);
}
$this->db->group_by('cd.Contract_Id ');
$this->db->order_by('cd.Contract_Id', 'desc');
$query = $this->db->get();
// print_r($this->db->last_query()); die;
if ( $query->num_rows() > 0 )
{
foreach($query->result() as $row){
$rows[] = $row;
}
return $rows;
}
}
My question is: I have also a table called company and my App is all about to print out all contracts files with their companies. I have defined Company_id as a foreign key in Contract_files.
Please help me to join company table to this function.
Thank you.
public function getallcontractfiles(){
$this->db->select('cd.*, GROUP_CONCAT(cf.Contract_File_Name) AS fileslink');
$this->db->from('contract_details as cd');
$this->db->join('contract_files as cf', 'cd.Contract_Id = cf.Contract_Id');
if($this->session->userdata('user_type') == 'ADMIN' && $this->session->userdata('user_group') == '' ){
$this->db->where('cd.Company_id',$this->session->userdata('company_id'));
}
if($this->session->userdata('user_type') == 'USER'){
$this->db->where('cd.users_id',$this->session->userdata('logged_user'));
}
$this->db->group_by('cd.Contract_Id ');
$this->db->order_by('cd.Contract_Id', 'desc');
$query = $this->db->get();
// print_r($this->db->last_query()); die;
if ( $query->num_rows() > 0 )
{
foreach($query->result() as $row){
$rows[] = $row;
}
return $rows;
}
}
Try This Code.
I'm not familiar with your ORM but this should make sense:
$this->db->join('company', 'cf.Company_id = company.Company_Id','INNER');
Right after your join with contract_files table, Also specify fields you want to retrieve in the from company table as well.
UPDATE
Since your query is bringing up multiple contract files then you will have to do the same with company table as every contract file could have many companies, If they all have the same company, Then the foreign key Company_id should be in contract_details table.
IMPORTANT
GROUP_CONCAT() has a limitation of 1024 by default, So it's not the best idea to put files names in it.
LEFT JOIN table2 ON table1.column_name = table2.column_name;
RIGHT JOIN table2 ON table1.column_name = table2.column_name;
INNER JOIN table2 ON table1.column_name = table2.column_name;
how to write simple query into codeigniter query using join righ?????
$query = $this->db->query("Select staff_permissions_list.perm_type,staff_permissions_list.permission_key,staff_permissions_list.permission_label,
staff_permissions_list.id, staff_role_permissions.permission_id as p_id,staff_role_permissions.role_id
FROM staff_role_permissions
RIGHT JOIN staff_permissions_list ON staff_role_permissions.permission_id=staff_permissions_list.id
AND staff_role_permissions.role_id=$id WHERE staff_permissions_list.perm_type=0
ORDER BY staff_permissions_list.id ASC
");
if ($query->num_rows() > 0) {
return $query->result_array();
}
$this->db->select('book_id, book_name, author_name, category_name');
$this->db->from('books');
$this->db->join('category', 'category.category_id = books.category_id', 'right');
$query = $this->db->get();
you can get data using this method of right join
How about that ?
$query = $this->db
->select("Select staff_permissions_list.perm_type,staff_permissions_list.permission_key,staff_permissions_list.permission_label,staff_permissions_list.id, staff_role_permissions.permission_id as p_id,staff_role_permissions.role_id")
->from("staff_role_permissions AS srp")
->join("staff_permissions_list AS spl","srp.permission_id = spl.id","right")
->where("spl.perm_type","0")
->where("srp.role_id",$id)
->order_by("spl.id","ASC")
->get();
i put the role_id to the where section - maybe you need to put it back (not sure what you want to achieve here)
I am joining 3 tables and fetching data from database. But my problem is that I have to use 2 more tables to query to fetch a number of likes and numbers of comments on a post.
The query I am using is:
function GetHomeDeals($limit,$start)
{
$this->db->from('tbl_coupons');
$this->db->where('coupon_status', 'active');
$this->db->join('tbl_stores','tbl_stores.store_id=tbl_coupons.coupon_store');
$this->db->join('tbl_users','tbl_users.user_id=tbl_coupons.coupon_postedby');
$this->db->limit( $start,$limit);
$this->db->order_by("coupon_id", "desc");
$query = $this->db->get();
//echo $this->db->last_query();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return false;
}
}
table structure of likes tables is:
like_id
like_by
like_on
table structure for comments
comment_id
comment_by
comment_on
comment
How can i add a count of likes and comments my function?
I just want total number of likes and count in result
like_on= coupon_id
comment_on=coupon_id
use this:
$query->num_rows();
example:
$query = $this->db->get('table');
$num = $query->num_rows();
I have following scenario:
user selects couple categories
user should see posts which belongs to these categories
post belong to one or more categories
I setup the database like this:
users -> category_user <- categories
posts -> categoriy_post <- categories
I managed to accomplish this, but I had to find all ids from these tables to find relevant posts. I need to make it simpler because this approach is blocking some other actions I need to do. This is my code:
$categoryIds = Auth::user()->categories;
$ids = array();
$t = array_filter((array)$categoryIds);
if(!empty($t)){
foreach ($categoryIds as $key => $value) {
$ids[] = $value->id;
}
}else{
return View::make("main")
->with("posts", null)
->with("message", trans("front.noposts"))->with("option", "Latest");
}
$t = array_filter((array)$ids);
if(!empty($t)){
$p = DB::table("category_post")->whereIn("category_id", $ids)->get();
}else{
return View::make("main")
->with("posts", null)
->with("message", trans("front.noposts"))->with("option", "Latest");
}
$postsIds = array();
foreach ($p as $key => $value) {
$postsIds[] = $value->post_id;
}
$t = array_filter((array)$postsIds);
if(!empty($t)){
$postIds = array_unique($postsIds);
$posts = Post::whereIn("id", $postsIds)
->where("published", "=", "1")
->where("approved", "=", "1")
->where("user_id", "!=", Auth::user()->id)
->orderBy("created_at", "desc")
->take(Config::get("settings.num_posts_per_page"))
->get();
return View::make("main")
->with("posts", $posts)->with("option", "Latest");
}else{
return View::make("main")
->with("posts", null)
->with("message", trans("front.noposts"))->with("option", "Latest");
}
How to do this properly without this bunch code?
Yes, there is Eloquent way:
$userCategories = Auth::user()->categories()
->select('categories.id as id') // required to use lists on belongsToMany
->lists('id');
if (empty($userCategories)) // no categories, do what you need
$posts = Post::whereHas('categories', function ($q) use ($userCategories) {
$q->whereIn('categories.id', $userCategories);
})->
... // your constraints here
->get();
if (empty($posts)) {} // no posts
return View::make() ... // with posts
Or even better with this clever trick:
$posts = null;
Auth::user()->load(['categories.posts' => function ($q) use (&$posts) {
$posts = $q->get();
}]);
if (empty($posts)) // no posts
return View... // with posts
Obviously, you can write joins, or even raw sql ;)
You can take those categories directly from the database from user records:
SELECT ...
FROM posts AS p
INNER JOIN category_post AS cp ON cp.id_post = p.id
INNER JOIN categories AS c on c.id = cp.id_category
INNER JOIN category_user AS cu ON cu.id_category = c.id
WHERE cu.id_user = 123 AND p.published = 1 AND ...
Joins in Laravel can be achieved, see the documentation: laravel.com/docs/queries#joins Maybe there is also an Eloquent way, I don't know, try searching :-)