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
Related
My two table Member and Deposit there has one to many relationship one member has multiple deposit in Deposit table i want to search by multiple column both table which will have to match.
This is my Member Table
1.id,
2.branch_id,
3.village_id,
4.user_id,
5.name,
6.phone,
7.email,
8........
My Deposit Table
1.meber_id,
2.user_id
3.deposit_date,
4.deposit_amount,
5.total_amount,
6..........
My Controller Code
$depo = Deposit::with(['member'=>function($query){$query->where('branch_id',$request->branch_id)->where('status','running')->get();}])->where('user_id',$request->user_id)->whereDate('deposit-date','>=',$from_date)->whereDate('deposit-date','<=',$to_date)->get();
if i do that then ....$query->where('branch_id',$request->branch_id)->get()..... section is not working please help me any one
Try this:
$depo = Deposit::whereHas('member', function($query) use ($request){
$query->where([
['branch_id' => $request->branch_id],
['status'=> 'running']
])
})
->where('user_id',$request->user_id)
->whereDate([
['deposit-date','>=',$from_date],
['deposit-date','<=',$to_date]
])->get();
Your question is quite ambiguous, but looks like you need to use the $request in the with function.
$deposits = Deposit::with(['member' => function ($query) use ($request) {
$query->where('branch_id', $request->branch_id)
->where('status', 'running')->get();
}])->where('user_id', $request->user_id)
->whereDate('deposit-date', '>=', $from_date)
->whereDate('deposit-date', '<=', $to_date)
->get();
But the with method wont filter down your query it will simply limit the number of members returned with all the deposits. It's not searching in the member table.
UPDATE 04/12/2018
Without checking the docs at all, and completely off the top of my head.
Deposit::with('member', function($query) use($request){
$query->where('branch_id', $request->branch_id)
->orWhere('village_id', $request->village_id)
})->where(function($query) use($request) {
$query->where('user_id', $request->user_id)
->whereDate('deposit-date', '>=', $from_date)
->whereDate('deposit-date', '<=', $to_date)
})->orWhereHas('member', function($query) use($request){
$query->where('branch_id', $request->branch_id)
->orWhere('village_id', $request->village_id)
})->get();
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');
In zend project I fetch data from the database and show in a table. It shows data in the same order as stored in the database. But I want to show the fileds in diffeent order than in database order. But I don't know how to do thid. Here I means field order not the row order.
Please help me in this regard.
Thanks
Its hard to say how youre displaying but lets assume you have made Table Classes for each table i would do something like this:
<?php foreach $zendDbRowObject->getTable()->getDisplayOrder() as $fieldName): ?>
<?php echo $zendDbRowObject->$fieldName; ?>
<?php endforeach; ?>
So then in your Table clas for a particular table you can create a property/method to get the fields in the order you want them in:
public function getDisplayOrder() {
// fake column names obviously... use yours here.
return array(
'column5',
'column1',
'column4',
'column2',
'column3'
);
}
when you do db select, you can select the coloumns that you want, so instead of select *,
you do select col5,col3,col2,col6,col1 from tablename
This will change the order of the columns
$select = $db->select()
->from(array('t' => 'table'),
array('t.col2', 'p.col1`'));
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,
i know i can define relationships through _referenceMap, i know that i con join selects trough
$db->select()
But what i need is to fetch rowset in model extending Zend_Db_Table_Abstract and then order it by value of referenced column from another table.
Is there some workaround to do that?
edit:
heres is the example:
first table:
table bugs columns id, bugname, authorid
second table:
table authors columns id, authorname
I have a model Model_Bugs extends Zend_Db_Table_Abstract
I want to make something like this:
$model->fetchAll($model->select()->order('authorname ASC'))
This means, that i need to join tables and sort by a column, which is not in the model table.
thanks for help
Jan
I would add a method in Model_Bugs like so:
public function fetchBugsByAuthorname() {
$bugTable = $this;
$bugTableName = $this->info('name');
$authorsTable = new Model_Authors();
$authorsTableName = $authorsTable->info('name');
$select = $bugTable->select()
->setIntegrityCheck(false)
->from($bugTable, array('id', 'bugname', 'authorid'))
->join($authorsTableName,
"$bugTableName.authorid = $authorsTableName.id",
array("authorname"))
->order("$authorsTableName.authorname asc");
$result = $bugTable->fetchAll($select);
return $result;
}
But to do this you have to turn off ZF's table integrity checking (setIntegrityCheck(false) above), which means you won't be able to directly call save() on the resulting rows. But if it's for a read-only purpose, it will work.
If you needed to save rowsets back to the database, you may have to first select the author ID's from Model_Authors in the order you want them, and then re-order your Model_Bugs query accordingly. It's messier but it can work.