FuelPHP - pre_save() on model not working - fuelphp

Does anyone knows how to get pre_save() or pre_validate() to work in my model?
I have the code below in the model, but it is never called.
protected function pre_validate($data)
{
//die("asefeg");
$data->idRegistrant = Session::get('idRegistrant', null);
return $data;
}

The ORM does not have a pre_save() method like the CRUD model does. For this functionality you want to look into implementing an observer. Plenty of info can be found about this in the fuelphp docs

Related

Shopware 6 Forms

Newbie question. How to handle Storefront Form Submit in Shopware 6? How to save the data from form to database? I have an entity, form shown in storefront and a controller but i have no idea how to save the data to entity. Thanks in advance.
you would have to be more specific with the description, of what exactly you are trying to achieve.
But in general, if you already have a controller, that receives the data, then you can get them from the request like this:
$data = $request->request->all();
By this, you have all the values from your form saved in an array $data. You have written, that you already have an entity, so from that I assume, that your entity is already mapped to your database table. So the only thing you have to do, is to use the repository to save the data. For thta, you just need to inject it into your class and get a context. The context depends on where you currently are, so for the purpose of the example, I have just created the default context.
It should look like this:
class MyClass
{
protected $myEntityRepository;
public function __construct(
MyEntityRepository $myEntityRepository
)
{
$this->myEntityRepository = $myEntityRepository;
$this->context = \Shopware\Core\Framework\Context::createDefaultContext();
}
public function myMethod ($data)
{
$this->myEntityRepository->upsert($data, $this->context);
}
}
Hope this helps. I have actually written an article on repositories in Shopware 6, so if you want to get some more information and examples, you can check it here: https://shopwarian.com/repositories-in-shopware-6/.

Ability to tell if a model for CRUD controller has been updated using Backpack for Laravel

I'm using a Backpack for Laravel CRUD controller and I'm trying to figure out if there's an efficient way to tell if a Model has been updated without loading both the current model and the updated model and then comparing attribute values.
I ended up following the suggestion in the comment and overrode the save() method on my model as such.
public function save(array $options = [])
{
// Update the mode accordingly and perform any
// other actions you need to prior to saving here.
$saved = parent::save($options);
return $saved;
}

Override Sails.js Waterline intercept Find method

I have a model named Product.
I want configurable across the board filtering for this model.
For example: sails.config.field = 2;
When I do GET /Product I want it to essentially do GET /Product?where={"field": 2}
The above works for blueprint by adding a policy, but I want consistent behavior when I use the waterline ORM
GET /Product
and Product.find() should return the same thing.
I can override the model: Product.find and it would work perfectly... IF there was some way for me to access the underlying find code.
The code I am using to intercept the blueprint is:
if (!req.query.where) {
req.query.where = `{"status":{">":0,">=":${sails.config.catalogVersions.status}}}`;
} else {
const parsedWhere = JSON.parse(req.query.where);
parsedWhere.status = {
'>': 0,
'>=': sails.config.catalogVersions.status,
};
req.query.where = JSON.stringify(parsedWhere);
}
I could very easily apply this to a Model.find interceptor.
Is there any way that once sails is loaded, I can access the root find method on a model even if its been overwritten at load time?
Maybe you could think on something like this one:
https://github.com/muhammadghazali/sails-hook-pagination
It's a hook and maybe works for your intended use.
I ended up using the hook:orm:loaded hook, to run some code which monkeypatched all of the models with a defaultScope which was stored in each of my models. It works well as I can easily modify the default criteria on all of the models and get consistent behavior across blueprint and waterline.
For code see:
Is there a way to override sails.js waterline endpoint with custom controller, but keep built in pagination, and filtering?

Agile Toolkit addExpression and DSQL

I have been following the book provided by the agile toolkit and am at this portion:
http://agiletoolkit.org/learn/app/logic
Unfortunately the dsql api lacks some clarity and the calculated fields example on that page is just not working.
I tried using:
class Model_DVD extends Model_Table {
public $table='dvd';
function init(){
parent::init();
$this->hasOne('Movie');
$this->addField('code');
$this->addfield('is_rented')
->type('boolean')
->calculated(true);
}
function calculate_is_rented(){
return $this->add('Model_Rental')
->dsql()
->field('id')
->where('rental.dvd_id=dvd.id')
->where('is_returned!=','Y')
->select()
;
}
}
This returns 1 every time so I tried to follow:
Calculated field always returns 1 - atk 4.2
And now I'm not sure how to create this properly; I know the following works but I kind of made it up as I went and have NO IDEA why it works, and why I need the count() - etc etc.
How do I go about getting the calculated field to work? Can someone please explain it in this example, I learn best by examples so I wanted to follow through with it.
My working code that I don't understand:
class Model_DVD extends Model_Table {
public $table='dvd';
function init(){
parent::init();
$this->hasOne('Movie');
$this->addField('code');
$this->debug();
$this->addExpression('is_rented')->set($this->add('Model_Rental')
->addCondition('dvd_id', $this->_dsql()->getField('id'))
->count()
->where('is_returned!=','Y'))
->datatype('boolean')
->enum(array('Y', 'N'));
}
}
Thanks!
DSQL APP tutorial is for Agile Toolkit 4.1.
Please watch the screencasts http://youtube.com/theagiletoolkit for updated and more extensive tutorial.

Problems with Doctrine 2.0

1-) In old version i use syncronizeWithArray method of Doctrine_Record, it's save a lote of time.
Version 2.0 does not provide it?
2-) In code below flush command generate 3 records on database, its a bug of 2.0?
public function indexAction()
{
$em = $this->getInvokeArg('bootstrap')->em
$obj = new Entity\Obj();
$obj->name = "teste";
$obj->last_name = "teste";
$em->persist($obj);
$em->flush();
}
Obs: The indexAction is called onlyOneTime.
Problem 2 Solved. its my fault!!
In version 2 doctrine entities don't extend from a base class anymore. Therefore the functionaly synchronizeWithArray isn't in the model anymore.
You could however implement ArrayAcces to work with Entities as if with arrays.
See implementing array access in the reference.