zend framework check if column exist in a resultset - zend-framework

I have this query for example:
$dbAdapter = Zend_Db_Table::getDefaultAdapter();
$query_Group = $dbAdapter->select();
$query_Group->from(array('FI' => 'request_field'),
array('*'));
$resultRows = $dbAdapter->fetchAll($query_Group);
Ok, now how can I know if inside $resultRows there is the column "Label" for example?
I know I can do that:
foreach($resultRowsas $key => $Field)
{
if(isset($Field['Label'])
{ .... }
}
But if is possible I want it to know without loop it....
It is possible?
Thanks again....

$Field['Label'] will always be set. It may be empty, but will always be set!
if you want all records where the value is NULL, change your query appropriately

If I understood correctly, you want to know whether a given column exists in the table. In that case, you might call the describeTable() method for this.
You can see a description in the Zend_Db_Adapter documentation.
If the column is defined in the table schema, then you need to query for an appropriate value, like NULL, as #JellyBelly says. In this case, his answer is what you need.
Hope that helps,

Related

columns of joined table missing

I'm missing the columns of a joined table. I have two tables like:
t_q_filialen 1 --> n t_mandant
I did my select statement in my model as follows:
$select= new Select (); //another try ('t_mandant','t_q_filialen');
$select->columns(['id', 'cccid' ,'geschid', 'name', 'langname','filiale']);
$select->from('t_mandant');
$select->join('t_q_filialen', 't_q_filialen.id=t_mandant.geschid ', [ 'filialeid'=>'id','filiale'=>'name']);
I expected to get the column name with the given alias 'filiale' like this in my view script:
$this->escapeHtml($mandant->filiale);
I get an error.
Unknown column 't_mandant.filiale' in 'field list'
If I change back to 'geschid' which is the foreign key-column in the table t_mandant and try:
var_dump(get_object_vars($mandant));
the columns from the table 't_q_filialen' are not in the collection.
What's wrong?
I have a small idea that I could be something with paginator. I instantiate paginator like this:
$resultSetprototype=new ResultSet();
$resultSetprototype->setArrayObjectPrototype(new Mandant());
$paginatorAdapter= new DbSelect($select, $this->tableGateway->getAdapter(), $resultSetprototype);
$paginator=new Paginator($paginatorAdapter);
return $paginator;
How can I change this, so that my join will be accepted? Might have something to do with this:
$resultSetprototype->setArrayObjectPrototype(new Mandant());
In other models without pagination I use this one and it works:
$rowset = $this->tableGateway->adapter->query($statment, "execute");
So which could be the solution? I really would prefer the last possibility, but I have no idea how to use it in combination with paginator.
Any help appreciated!
I listed several ideas, but my goal would be, to have the columns of both tables in my collection with the paginator-object.
oh my god, I just tried:
$paginatorAdapter= new DbSelect($select, $this->tableGateway->getAdapter());
Without the resultsetprototype

Runtime error: MongoDB::DatabaseError: bad hint

I am trying to call the hint method on a MongoDB::Cursor object. However, it throwing an exception when it's trying to execute the query. See the code sample below:
sub some_method_which_returns_cursor {
my $cursor = $collection->find($filter);
if ($hint) {
$cursor->hint({‘some_index’ => 1}); #failing here.
}
if ($sort) {
$cursor->sort($sort);
}
return $cursor;
}
Any thoughts as to what's going on and how I can fix this?
Harish asked me via email and I'll repeat my answer here for posterity:
The hint method takes a string when given an index name, or an array reference when given keys/order pairs:
$cursor->hint("some_index"); # by name
$cursor->hint([field1 => 1, field2 => -1]); # by keys
It also takes a hash reference, but don't use that because modern Perls randomize key order when serializing, so your hint may not match an index.

Extbase "findBy" with multiple parameters

I got an extension in which i want to include some filters, i know figured out that i can filter the results that are shown of my listAction() by using findBy.
I tested it and it worked like this:
$cars = $this->carRepository->findByCarid("1");
$this->view->assign('cars', $cars);
My problem now is i need to filter the result with more than one Parameter, what if i want to add findByColor("blue") so it gives me all cars wit hid 1 and color blue? What solution does extbase have for that kind of search queries? i can`t find anything good or understandable in the documentation.
You have to extend you repository and code this functionality on your own. Extbase offers you a simple but powerful API to do so.
class whatEverYourRepositoryIsCalled extends \TYPO3\CMS\Extbase\Persistence\Repository {
public function findByFilter($carId, $color) {
// Create empty query = select * from table
$query = $this->createQuery();
// Add query options
return $query->matching(
// ALL conditions have to be met (AND)
$query->logicalAnd(
// table column carId must be euqal to $carId
$query->equals('carId', $carId),
// table column color must be euqal to $color
$query->equals('color', $color)
)
);
}
}
This is a quite simple approach to your problem. In a real world scenario I would probably use an array of filter criteria to do the filtering like array('carId' => 1, 'color' => 'blue'). Inside of findByFilter() those values would be extracted and added to the query.
The key is to build the desired query. A quite comprehensive explanation of how to do that can be found at http://blog.typoplanet.de/2010/01/27/the-repository-and-query-object-of-extbase/. Unfortunately it's not completely up to date but the part about constructing queries is still valid.

Laravel and Eloquent: Specifying columns in when retrieving related items

This is a followup post to: Laravel 4 and Eloquent: retrieving all records and all related records
The solution given works great:
$artists = Artist::with('instruments')->get();
return \View::make('artists')->withArtists($artists);
It also works with just:
$artists = Artist::get();
Now I'm trying to specify the exact columns to return for both tables. I've tried using select() in both the statement above and in my Class, like this:
ArtistController.php
$artists = Artist::select('firstname', 'lastname', 'instruments.name')->get();
or:
$artists = Artist::with(array('instruments' => function($query) {
$query->select('name');
}))->get();
(as suggested here and while this doesn't throw an error, it also doesn't limit the columns to only those specified)
or in Artist.php:
return $this->belongsToMany('App\Models\Instrument')->select(['name']);
How would I go about getting just the firstname and lastname column from the artists table and the name column from instruments table?
Not sure what I was thinking. I think working on this so long got me cross-eyed.
Anyhow, I looked into this a lot more and searched for answers and finally posted an issue on GitHub.
The bottom line is this is not possible as of Laravel v4.1.
https://github.com/laravel/laravel/issues/2679
This solved it:
Artists.php
public function instruments() {
return $this->hasMany('App\Models\Instrument', 'id');
}
Note that I changed this to a hasMany from a belongsToMany which makes more sense to me as a musicians (or Artist) would have many Instruments they play and an Instrument could belong to many Artists (which I also alluded to in my previous questions referenced above). I also had to specify 'id' column in my model which tells the ORM that instrument.id matches artist_instrument.id. That part confuses me a bit because I thought the order for hasMany was foreign_key, primary_key, but maybe I'm thinking about it backwards. If someone can explain that a bit more I'd appreciate it.
Anyhow, the second part of the solution...
In ArtistsController.php, I did this:
$artists = Artist::with(array(
'instruments' => function($q) {
$q->select('instruments.id', 'name');
})
)->get(array('id', 'firstname', 'lastname'));
That gives me exactly what I want which is a collection of Artists that contains only the firstname and lastname columns from the artists table and the name column for each of the instruments they play from the instruments.
$artists = Artist::with(array('instruments' => function ($query) {
$query->select('id', 'name');
}))->get('id', 'firstname', 'lastname');

How to populate a property that is not directly stored in Database with CodeFirst

I have 2 Entities, each of which is managed by EF Code First, and each happily sitting in its own table. Entity_A has a property, called "SumTotal", which should be the sum of a specific column in Entity_B that matches a certain criteria.
SumTotal should not be persisted in the DB, but rather calculated each time an instance of Entity_A is retrieved.
I have looked at ComputedColumns, but it appears that the computedcolumn can only be defined relative to columns in the same table.
I also have a feeling that I need to set SumTotal to NotMapped (or something similar with AutoGenerated), but dont know how to get the actual value into SumTotal.
Hope this question makes sense, thanks in advance
You can project the results to an anonymous object and transform that it to your entity
var projection = db.EntityAs.Where(/* */)
.Select(a => new {A = a, Sum = a.Bs.Sum(b => b.Total)})
foreach(p in projection)
{
p.A.SumTotal = p.Sum;
}
var As = projection.Select(p => p.A);