Symfony2 Doctrine, How to fetch only the column names of particular table - php

Hi i want only the table field names(column names) how to achieve that, as of now i do have the following code -
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('SkerpInventoryBundle:InventoryMaster')->findAllIndex();
I am using the repositories to fetch all the data of the table. Now in "$entities" I am having the result data. Can anyone let me know how to fetch only the field name.

I guess, this is what you need (assume that you are in your controller):
$mappings = $this->getDoctrine()->getManager()->getClassMetadata('AcmeBundle:SomeEntity');
$fieldNames = $mappings->getFieldNames();

You can use DQL (Doctrine Query Language) For this. For example:
$query = $em->createQuery('SELECT u.username FROM CmsUser u');
$users = $query->getResults(); // array of CmsUser username values
echo $users[0]['username'];
For more information on DQL see:
http://docs.doctrine-project.org/en/2.1/reference/dql-doctrine-query-language.html

Related

How to get specific columns in CodeIgniter 4?

I wanna select specific columns from table. Not all columns.
$this->userModel->where($where)->first();
I'm using this but it returns all data.
According to the CI4 query builder documentation you can use select() in this way:
$db = \Config\Database::connect();
$builder = $db->table('mytablename'); // 'mytablename' is the name of your table
$builder->select('userid, username'); // names of your columns, single string, separated by a comma
$builder->where('userid', 5); // where clause
$query = $builder->get();
return $query;
where() could be possible to use in 4 ways, you can choose.
This stuff should be placed in Model file, method of which would return $query set of DB data.
(Tested under CI 4.2.7)
There is also a select method in the model, which accepts an array of columns:
$userModel->select(['id','name'])->where('id', 1)->first();

Build a query inside another with querybuilder?

I got 2 tables User and Group; each object Group got an array members[] that points to some instances of User.
I need to build this same query with QueryBuilder :
SELECT (users instances) FROM User
WHERE (users instances) IN (SELECT Group.members from Group WHERE group.id = $someId)
How can I achieve that?
You have to make 2 querybuilders:
$qb2 = $this->em->createQueryBuilder('group')
->select('group.members')
->where('group.id = $someId');
$qb = $this->em->createQueryBuilder('user')
->select(user instances)
-where($qb->expr()->in('user instances', $qb2->getDQL());
it will give you the idea how it works. Of course you have to adjust this code to yours.
Improving #Eimsas Answer it could be:
$em = $this->getEntityManager();
$qb2 = $em->createQueryBuilder('group')
->select('group.members')
->where('group.id = $someId');
$qb = $em->createQueryBuilder('user');
$query = $qb->select(user instances)
->where($qb->expr()->in('user instances', $qb2->getDQL());
$result = $query->getQuery->getResult();

join two tables in codeigniter and refere to two fields in the same table

My application works like this: α user creates a task and assigns it
to another user. I have two tables
(user table)
user_name
AND
(task table)
task_subject
creator
assigned_to
my question how to make join statement by codeigniter to display
the task, its creator name and assigned to whom.
Please check following query for join in codeigniter
$user = 'user';
$task = ' task,';
$this->db->select($user.'.*,'.$task.'.*');
$this->db->from($task);
$this->db->join($user,
$task.'.user_id = '.$user.'.user_id'
);
$this->db->join($user,$user.'.user_id = $task.'created_by','left');
$query = $this->db->get();
if ($query->num_rows() > 0){
$result = $query->result();
return $result;
}
return FALSE;
Instead of using * you can use your own custom fields that you want to show and in join use the common id in both tables like I use user_id

Column Name Conflict on Laravel 4 Fluent Query Builder

I have a query like this:
$users = DB::table('users')->join('user_roles','users.role_id','=','user_roles.id')->get();
and a table that has a column id (users.id) and another table that has columns id and user_id (user_roles.id & user_roles.user_id),
but the problem is.. what is being returned on $user->id is the user_roles.id instead of the users.id column.. how do i fix this so that what i get is not the role id but the user id instead..
thanks!
Found it!
using ->select('users.*', 'user_roles.role_name') i was able to remove user_roles.id from the returned values and thus eliminating the conflict.
Here is the final query:
$users = DB::table('users')->join('user_roles','users.role_id','=','user_roles.id')->select('users.*', 'user_roles.role_name')->get();
The better way to do this is using 'as':
$users = DB::table('users')->join('user_roles','users.role_id','=','user_roles.id')->get(array('users.*', '**user_roles.id as user_roles_id**', 'user_roles.*'));

codeigniter join 2 table data

hi everyone i am new to codeigniter and currently working on a small project in the project i am trying to join two tables and display there data in single table. i looked at the user guide that codeigniter has an i am not sure how this work
$this->db->join();
what table should be first and what id key should be firs. Can someone explain me more in detail about this please use examples if u can. I am trying to join credential table and tblanswers. Tnx for answering.
i have tried to code a function using this example:
$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();
EDIT:
instead of using join method in codeigniter is it possible to use a simple function to retrieve the two table data separately? all i want is to echo the data from database table on to a html table in my website page to be displayed is it possible to write two get functions to retrieve two tables separately ?
It doesn't matter what table is first... Simply:
<?php
$this->db->select('t1.name, t2.something, t3.another')
->from('table1 as t1')
->where('t1.id', $id)
->join('table2 as t2', 't1.id = t2.id', 'LEFT')
->join('table3 as t3', 't1.id = t3.id', 'LEFT')
->get();
?>
here is how it works:
suppose we have two tables namely student, library.
note: but remember that one of the column should match if you want to use where condition/ here we assume that both tables have std_id column
Write the the select query as follows, in the brackets write what are all the things you want
note:write as shown below don't put quotes to each single one put it on whole at once.
*note: suppose we want name, phone_no. from student table and book_name form library table.*
$this->db->select('name, phone_number, book_name');
Now write the from query and write one of the table name(No rule)
$this->db->from('student');
Now join this with the another table with join query
$this->db->join('library', 'students.std_id=library_std_id');
Now write the where condition that like you want book name form library table where std id=1(in practical you need to fetch this id from view/database)
$this->db->where('std_id', 1);
$q= $this->db->get();
That's it it's done now you can print and check the result.
$this->db->join('comments', 'comments.id = blogs.id');
With this line you tell: search me inside comments all comments with id equal blogs.id.
Usually is something like that I think:
$this->db->join('comments', 'comments.blogs_id = blogs.id');
You have to insert into your table a field named blogs_id (int value unisgned) because blogs can have more comments.
Isn't important the position of first or second value
Hi this will work for joining two tables in codeIgnator.
$this->db->select("chat.id,chat.name,chat.email,chat.phone,chat.date,post.post");
$this->db->from('chat');
$this->db->join('post', 'chat.id = post.id');
$query = $this->db->get();
if($query->num_rows() != 0)
{
return $query->result();
}
else
{
return false;
}
You can change to the query as you like do trail and error method to get your appropriate results.
This is my code for joint many tables as possible.
I have 3 tables professeurs,publications and support.
public function toutesdonnées(){
$this->db->select("*");
$this->db->from('publication');
$this->db->join('support', 'publication.idsup = support.idsup');
$this->db->join('professeurs', 'publication.emailprof = professeurs.emailprof');
$query = $this->db->get();
if($query->num_rows() != 0)
{
return $query->result();
}
else
{
return false;
}

Categories