sfWidgetFormDoctrineChoice: bug, error or what? - symfony1

I'm using sfWidgetFormDoctrineChoice to build select elements in a form. This is how I'm using:
// Fill maquinaemisorid
$this->widgetSchema['maquinaemisorid'] = new sfWidgetFormDoctrineChoice(array('model' => 'SdrivingMaquina', 'add_empty' => 'Seleccione una Máquina', 'table_method' => 'fillChoice'));
// Fill idoperador
$this->widgetSchema['idoperador'] = new sfWidgetFormDoctrineChoice(array('model' => 'SdrivingOperador', 'add_empty' => 'Seleccione un Operador', 'table_method' => 'fillChoice'));
// Fill idturno
$this->widgetSchema['idturno'] = new sfWidgetFormDoctrineChoice(array('model' => 'SdrivingTurno', 'add_empty' => 'Seleccione un Turno', 'table_method' => 'fillChoice'));
For the first widget all is fine and values are taken from DB as should be but for the second one and the third is not working. As you may notice I'm calling a method fillChoice in all widgets table_method. This is the code for the three:
SdrivingMaquinaTable.class.php
public function fillChoice() {
$id_empresa = sfContext::getInstance()->getUser()->getGuardUser()->getSfGuardUserProfile()->getIdempresa();
return Doctrine_Core::getTable('SdrivingMaquina')->createQuery('a')->where('a.idempresa = ?', $id_empresa)->execute();
}
SdrivingOperadorTable.class.php
public function fillChoice() {
$id_empresa = sfContext::getInstance()->getUser()->getGuardUser()->getSfGuardUserProfile()->getIdempresa();
return Doctrine_Core::getTable('SdrivingOperador')->createQuery('a')->where('a.idempresa = ?', $id_empresa)->execute();
}
SdrivingTurno.class.php
public function fillChoice() {
$id_empresa = sfContext::getInstance()->getUser()->getGuardUser()->getSfGuardUserProfile()->getIdempresa();
return Doctrine_Core::getTable('SdrivingTurno')->createQuery('a')->where('a.idempresa = ?', $id_empresa)->execute();
}
The method is the same always just changes the table used for each class. I check the generated queries and all are fine and if I run each on phpMyAdmin for example more than one value is fetched altough in idoperador and idturno just the latest is rendered. This is a very rare behavior and I can't find the error or mistake so I need some help or advice here.
As an adition I tried this in SdrivingRegistrosEmisoresForm.class.php (this is where the widgets are being generated):
$id_empresa = $this->current_user->getSfGuardUserProfile()->getIdempresa();
$choices_operador = Doctrine_Core::getTable('SdrivingOperador')->createQuery('a')->where('a.idempresa = ?', $id_empresa)->execute();
$this->widgetSchema['idoperador'] = new sfWidgetFormSelect(array('choices' => $choices_operador));
And this way works fine but the id for each item fetched isn't right meaning:
id item real_id
0 Item1 2
1 Item2 16
2 Item4 3
I talked about this in this topic but didn't get any answer, any help?

As stated in sfWidgetFormDoctrineChoice, if option *table_method* returns a collection like your methods, it renders choices with options *key_method* and method. By default , they are respectively getPrimaryKey() and *__toString()*.
Can we check you schema.yml and the __toString() ?
As for your last example the option choices of sfWidgetFormSelect should be an array an not a *Doctrine_Collection*

Related

How to handle the result of a list of future with ReasonML?

I am trying to go over a list of items, cache the URL and then update the URL in the list before drawing it.
However I don't seems to be able to do so.
Any idea?
external cache: option(string) => Js.Promise.t({. "uri": string, "filePath": string }) = "get";
let items = result##data##data->Option.getWithDefault([||]);
let cachedUri = items
->Belt.Array.map(
item =>
cache(item##hosted_video_url)
->FutureJs.fromPromise(Js.String.make)
->Future.flatMapOk(cachedObj => (
{. "hosted_video_url": cachedObj##uri }
)
))
->Belt.Array.toList
->Future.all;
The idea is that you cannot exit a future, so you need to update the state inside the future thing rather than trying to get an equivalent of an asyncio.gather or something similar.
I changed:
setVerticalData(_ => {items-> Js.Array2.sortInPlaceWith((a, b) => {...})});
with
items
->List.fromArray;
->List.map(item =>
cache(item##hosted_video_url)
->FutureJs.fromPromise(Js.String.make)
)
->Future.all
->Future.map(cachedList => {
setVerticalData(_ => {items: cachedList})
})

How to change value of one column in a specific record in a table EF MVC

I have the ID of a record I would like to update, However I would only like to edit just one field in this record.
I would like to change the "Availability" field value in the "Vehicles" table from True to False.
How can I go about doing this without changing any other values?
I am using the code first approach and vehicleID holds the Id of the record I want to edit in the "Vehicles" table
var vehicleID = db.RentalAgreements
.Where(x => x.rent_agree_no == merchant_reference)
.Select(x => x.vehicle_id)
.Single();
var d = db.RentalAgreements.Where(x => x.rent_agree_no == merchant_reference).Select(x => x.vehicle_id).Single();
var vehicle_record = db.Vehicles.FirstOrDefault(p => p.vehicle_id == Convert.ToInt32(d));
vehicle_record.Availability = false;
db.SaveChanges();

Laravel Model Factory Error: Trying to get property of non-object

I'm trying to use a model factory to seed my database, but when I run it I get the error:
Trying to get property 'id' of non-object
Here is my code:
// TasksTableSeeder.php
factory(pams\Task::class, '2000', rand(1, 30))->create();
// ModelFactory.php
$factory->defineAs(pams\Task::class, '2000', function (Faker\Generator $faker) {
static $task_number = 01;
return [
'task_number' => $task_number++,
'ata_code' => '52-00-00',
'time_estimate' => $faker->randomFloat($nbMaxDecimals = 2, $min = 0.25, $max = 50),
'work_order_id' => '2000',
'description' => $faker->text($maxNbChars = 75),
'action' => '',
'duplicate' => '0',
'certified_by' => '1',
'certified_date' => '2015-11-08',
'status' => '1',
'created_by' => '1',
'modified_by' => '1',
'created_at' => Carbon\Carbon::now()->format('Y-m-d H:i:s'),
'updated_at' => Carbon\Carbon::now()->format('Y-m-d H:i:s'),
];
});
I've tried removing all variables from the model factory and using constants, but that doesn't fix it. I've tried pulling the data from the ModelFactory.php and put it directly into the TasksTableSeeder.php and it does work, however I was using constants and no variables.
I cannot for the life of me figure out what 'id' it's talking about.
I'm running Laravel v5.1
Your BaseModel is not exactly appropriate as it will force you to create hacks in order to run something like a unit test. It will be better that you have a flag in the BaseModel that you test for true or false before setting created_by and modified_by.
Also there's no guarantee there will be user_id '1' at any point in time, unless you actually create that User in your factory first, before creating the Task.
A way to fix your current setup is have a protected field like $enableAuthUpdates which is set to false or true by default. Then you can override the field in any of your derived models such as your Task model to prevent the creating/updating events from running.
Also it's important to make sure your factory has an actual user it is working with and create it if it doesn't exist.
I found the problem. At one stage I had implemented a BaseModel.php model that automatically inserts the current users ID when creating or updating the model. The seeder fails because there isn't a current user, so I had to add a check to see if there was a user logged in first.
Here is the code:
static::creating(function($model)
{
if(Auth::user())
{
$model->created_by = Auth::user()->id;
$model->modified_by = Auth::user()->id;
}
else
{
$model->created_by = '1';
$model->modified_by = '1';
}
});
static::updating(function($model)
{
if(Auth::user())
{
$model->modified_by = Auth::user()->id;
}
else
{
$model->modified_by = '1';
}
});
It's not super pretty, but it gets the job done :)

Entity Framework n-to-n query, get last records from navigation table

I am using entityframework in my project.
I have 3 tables which are navigated with many to many relationship.
This is my diagram.
I want to select all my counters id which have last approve status == 15.
I wrote query like this;
var sayacOnayDurumlari =
db.CounterApproveStatus
.Where(x => x.ApproveStatusId == 15).OrderByDescending(x=>x.Id)
.GroupBy(x => x.CountersId)
.Select(e => e.FirstOrDefault());
but it takes my older records which are ID == 15
var son =
db.Counters.Where(
x => x.CounterApproveStatus.OrderByDescending(t => t.Id).FirstOrDefault().ApproveStatusId == 15)
.ToList();
I tried this and I supposed I achieved it. Is it a good query?
You need group first, then find if the latest Id in that group has desired statusId.
Following syntax may not be exactly right, but you could get idea.
var sayacOnayDurumlari =
db.CounterApproveStatus
.GroupBy(x => x.CountersId)
.Select(g => new {
CountersId = g.Key,
LatestRecord = g.OrderByDescending(x=> x.Id)
.FirstOrDefault()
})
.Where(g=> g.LatestRecord.ApproveStatusId == 15)
.Select(g => g.CountersId).ToList();

ZF2 tableGateway select

I started with the ZendSkeletonApplication and added a model extending Zend\Db\TableGateway\TableGateway.
I have the following method:
public function findByType($type) {
$rowset = $this->select('type' => $type);
return $rowset;
}
This works, but now if i do this:
$foo = $table->findBytype('foo');
$bar = $table->findBytype('bar');
the first one works, the query it executes is:
SELECT * FROM table WHERE 'type' = 'foo'
The second one however executes the following query:
SELECT * FROM table WHERE 'type' = 'foo' AND 'type' = 'bar'
is this expected behavior?
If so how can i have the second time i call the method execute the following query:
SELECT * FROM table WHERE 'type' = 'bar'
thanks in advance!
Should use select in tableGateway like this:
$select = $this->getSql()->select();
$select->where(array('type' => 'foo'))
->where(array('type' => 'bar'));
$rowset = $this->selectWith($select);
select() will be reset the where() paramters when you call it next time.
See more usage in my blog:
http://avnpc.com/pages/advanced-database-select-usage-in-zf2

Resources