Zend selects all columns - php

In the following code:
$selectColumns= array('user_id.user_email', // inner join the data from user_id and user_details
'user_details.first_name',
'user_details.last_name');
$result = $handle->select()->from('user_id', $selectColumns)
->where('user_id.uid=?', $uid)
->join('user_details', 'user_id.uid = user_details.uid')
->query(ZEND_DB::FETCH_OBJ);
Zend selects all the columns in the table, not just the requested ones.
How can I select only some?

The problem is in your join() method call:
->join('user_details', 'user_id.uid = user_details.uid')
The optional third argument is columns from this table. If the argument is absent, it defaults to user_details.*.
Note that you added qualified columns from both tables in the from() table, but this has no effect on the default of user_details.*. Sorry, but Zend_Db_Select just isn't smart enough to keep track of all that.
You can make the join() call add no columns by passing an empty array:
->join('user_details', 'user_id.uid = user_details.uid', array())
The qualified columns you added in the from() call should still be there. To verify this yourself, print the SQL:
print $result . "\n"; // calls __toString() method on Zend_Db_Select object

Add another parameter to your join at the end -- an empty array. That will tell it to select no columns from the join. With the code you have now, you are selecting all columns from the joined table.
->join('user_details', 'user_id.uid = user_details.uid', array())

Related

Laravel filtering - get the last created entry from the DB which has a specific ID

For this data (from a table called status_student):
I want to do some filtering. This table is a pivot table and I want to return a record from a base table only if the latest created object has a specific status_id.
For example, if I filter for status_id = 1, I shouldn't get any object for those two rows.
Here's my query for filtering:
$query = Student::whereHas('statusuri', function($q) use ($status) {
$q->orderBy('status_student.data_sfarsit', 'desc')->where('status_id', '=', $status);
});
statusuri From Student:
public function statusuri()
{
return $this->belongsToMany(Status::class, 'status_student')
->withPivot('id', 'data_inceput', 'data_sfarsit', 'document', 'status_id', 'stare_stagiu_id')
->withTimestamps();
}
My query works, but it returns an object if I filter for a status_id of 1 and of 6 too. I only want to return an object for filtering with status_id = 6 (because that is the latest status).
I tried modifying my query like this:
$query = Student::whereHas('statusuri', function($q) use ($status) {
$q->orderBy('status_student.data_sfarsit', 'desc')->first()->where('status_id', '=', $status);
});
but then it didn't work, and I think I know the reason.
first() returns an instance of another type (after an inner join, basically), which doesn't have a status_id. So I shouldn't use it this way. Also, it returns an object, not a Query Builder instance anymore.
The error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'students.id' in 'where clause' (SQL: select * from `statusuri` inner join `status_student` on `statusuri`.`id` = `status_student`.`status_id` where `students`.`id` = `status_student`.`student_id` order by `status_student`.`data_sfarsit` desc limit 1)
So, how can I make that query filter my latest created DB row with a specific status_id?
Thanks.
//Example:
For student_id = 1 as in the picture:
if status_id = 1:
query returns nothing
if status_id = 6:
query returns the student
Explanation:
$status_id is provided through a HTML form, and I want to return that specific student if the last status known is equal to this $status_id.
My approach was filtering with latest(id) so I get the latest entry on the first row, then getting the first entry, then doing a where clause on the status_id. But it doesn't work because first() returns an object, not a QueryBuilder anymore.

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();

Select only calculated columns in Propel

I'm trying to perform a selection with Propel which returns only calculated columns, but I allways have other columns selected.
For example:
$criteria = new MuestraQuery();
$criteria->clearSelectColumns()
->addAsColumn('numEspesores', 'count(distinct muestra.sal_espesor)')
Resulting query:
SELECT muestra.sal_id, muestra.sal_regimen,
-- ...
-- (ALL FIELDS OF THE TABLE HERE...)
-- ...
count(distinct muestra.sal_espesor) AS numEspesores
FROM muestra
I've been able to reduce the number of fields selected including only a field. For example, this query returns only two fields:
$criteria = new MuestraQuery();
$criteria->clearSelectColumns()
->select(MuestraTableMap::COL_SAL_ID)
->addAsColumn('numEspesores', 'count(distinct muestra.sal_espesor)')
Resulting query:
SELECT count(distinct muestra.sal_espesor) AS numEspesores,
muestra.sal_id AS "muestra.sal_id"
FROM muestra
¿Is there a way in Propel to select only computed columns?
I've seen columns are added in ModelCriteria->doSelect() based on protected ModelCriteria->isSelfSelected property, which is set in ModelCriteria->select() but not in addAsColumn() because it's from Criteria and it's not overriden in ModelCriteria.
Don't know if this is a bug or I'm doing something badly.
Just select the computed column that you added.
MuestraQuery::create()
->select(['numEspesores'])
->addAsColumn('numEspesores', 'count(distinct muestra.sal_espesor)')
->find();
Due to Propel's fluent api, you do not have to call the select method prior to the addAsColumn method, so you could even do the following:
MuestraQuery::create()
->addAsColumn('numEspesores', 'count(distinct muestra.sal_espesor)')
->select(['numEsesores'])
->find();

querybuilder join query in Doctrine2 for Symfony2

Could somebody convert this query for me in querybuilder?
SELECT m.id,n.unitid
FROM mappaths m JOIN unitids n on (m.id=n.id) where n.databaseid=1
I am using this query but it gives me all the values of mm.unitid, while my requirement is to get only one value that is defined by test=1 variable
$query=$qb->select('mm.unitid')
->from('ApiMapBundle:Mappaths','m')
->from('ApiMapBundle:Unitids','mm')
// ->leftJoin('m','u')
->leftJoin('m.refUnitids1','u','WITH','m.id = u')
// ->leftJoin('m.refUnitids2','v')
->where('m.id=:test')
->setParameter('test',1)
->getQuery()->getResult();
Try following:
$query = $qb->select('mm.unitid')
->from('ApiMapBundle:Mappaths','m')
->innerJoin('m.refUnitids1','mm','WITH','m.id = mm.FIELD') //you need to specify on which field of mm join should be done
->where('m.id=:test')
->setParameter('test',1)
->getQuery()
->getResult();
You need to specify field of Unitids which should be used to join to Mappaths. The best way would be to define this relation in Entity definition, then you can use just ->innerJoin('m.refUnitids1','mm') without additional join parameters.
Also, in this case, it is better to use innerJoin instead of leftJoin

Inner join single column using Eloquent

I am trying to get a single column of an inner joined model.
$items = Item::with('brand')->get();
This gives me the whole brand object as well, but I only want brand.brand_name
$items = Item::with('brand.brand_name')->get();
Didn´t work for me.
How can I achieve this?
This will get related models (another query) with just the column you want (an id, see below):
$items = Item::with(['brand' => function ($q) {
$q->select('id','brand_name'); // id is required always to match relations
// it it was hasMany/hasOne also parent_id would be required
}])->get();
// return collection of Item models and related Brand models.
// You can call $item->brand->brand_name on each model
On the other hand you can simply join what you need:
$items = Item::join('brands', 'brands.id', '=', 'items.brand_id')
->get(['items.*','brands.brand_name']);
// returns collection of Item models, each having $item->brand_name property added.
I'm guessing Item belongsTo Brand, table names are items and brands. If not, edit those values accordingly.
Try this:
$items = Item::with(array('brand'=>function($query){
$query->select('name');
}))->get();

Categories