CakePHP Behaviors call to behavior method returns error - cakephp

I've been working with cakephp for personal use. Now I understood that I want to create some functions that will be repeated in many models of my project, so I found out by Cakephp docs that the best way to do it is using Behaviors.
Objective:
Each time a new entity of my project models are created, I want to notice some users(coworkers) by email. Like a new project added, new task added, etc...
What achieved until now
I created a behavior with an afeterSave event listener; I attached it in one of my models.
When I create I add a new task it runs the behaviors methods and send a simple email.
Problem
The problem is to find out witch model has called the event
I read the cake2.x docs and the afterSave event used to receive the event and the model in witch you could call alias to know the model name:
public function afterSave(Model $model,...){
$model_name = $model->alias;
if ($model_name == 'some_model'){
//code for specific model
} else (...)
}
However in cakephp 3.9 we receive Event and EntityInterface:
public function afterSave(Event $event, EntityInterface $model)
{
# I tried $mail_HTMLmessage= $model->_registryAlias;
# I tried $mail_HTMLmessage= $model->_className;
# I tried $mail_HTMLmessage= $model->get('className');
$this->sendMail($mail_HTMLmessage);// this is another method that i defined in my behavior that sends an email to me with the string mail_HTMLmessage as the mail message.
return true;
}
I've tested mail_HTMLmessage=$event->getName() and in my email I received event.afterSave as expected.
However everything I tried to get model/class name it returned an empty string.
Is it possible to get the model name? and what is the best way to do it?
Thanks in advance

I believe what you're looking for is not in the event at all, but the entity. getSource(), to be specific.

Related

Do Fluent conventions break lazy loading? (uNhAddIns)

I have a simple entity class in a WPF application that essentially looks like this:
public class Customer : MyBaseEntityClass
{
private IList<Order> _Orders;
public virtual IList<Order> Orders
{
get { return this._Orders; }
set {this._Orders = new ObservableCollection<Order>(value);}
}
}
I'm also using the Fluent automapper in an offline utility to create an NHibernate config file which is then loaded at runtime. This all works fine but there's an obvious performance hit due to the fact that I'm not passing the original collection back to NHibernate, so I'm trying to add a convention to get NHibernate to create the collection for me:
public class ObservableListConvention : ICollectionConvention
{
public void Apply(ICollectionInstance instance)
{
Type collectionType =
typeof(uNhAddIns.WPF.Collections.Types.ObservableListType<>)
.MakeGenericType(instance.ChildType);
instance.CollectionType(collectionType);
}
}
As you can see I'm using one of the uNhAddIns collections which I understand is supposed to provide support for both the convention and INotification changes, but for some reason doing this seems to break lazy-loading. If I load a custom record like this...
var result = this.Session.Get<Customer>(id);
...then the Orders field does get assigned an instance of type PersistentObservableGenericList but its EntityId and EntityName fields are null, and attempting to expand the orders results in the dreaded "illegal access to loading collection" message.
Can anyone tell me what I'm doing wrong and/or what I need to do to get this to work? Am I correct is assuming that the original proxy object (which normally contains the Customer ID needed to lazy-load the Orders member) is being replaced by the uNhAddIns collection item which isn't tracking the correct object?
UPDATE: I have created a test project demonstrating this issue, it doesn't reference the uNhAddins project directly but the collection classes have been added manually. It should be pretty straightforward how it works but basically it creates a database from the domain, adds a record with a child list and then tries to load it back into another session using the collection class as the implementation for the child list. An assert is thrown due to lazy-loading failing.
I FINALLY figured out the answer to this myself...the problem was due to my use of ObservableListType. In NHibernate semantics a list is an ordered collection of entities, if you want to use something for IList then you want an unordered collection i.e. a Bag.
The Eureka moment for me came after reading the answer to another StackOverflow question about this topic.

How to get Business Rules from Entity Object programatically?

In my Fusion Web Application, I have defined several business rules in entity objects. Everything works fine. The problem is that I can not get them programatically. I have searched through the EntityObjects Impl java class, but there is no method that should perform validation. Does anyone know any way, how to get the business rules from an entity object? I need to get at least a list of those.
Update:
EntityDefImpl eoDef = EntityDefImpl.findDefObject("package...MyEO");
for (Object o : eoDef.getValidators()) {
System.out.println("Rule: " + o);
}
But even in this case, I do not get a list of business rules.
Try the following instead of your implementation
EntityDefImpl eoDef = EntityDefImpl.findDefObject("package...MyEO");
AttributeDefImpl myAttribute=getAttributeDefImpl("MyAttribute"); //Get the first Attribute
for (Object o : myAttribute.getValidators()) {
System.out.println("Rule: " + o);
}
The one you did will get the Entity level validators only, this one will get you this specific attribute validators!
Take a look at the EntityDefImpl class. Since it applies to all EO instances it carries the validation.enter link description here
If you just want to call it, you can use the Validate function from ViewObjectImpl (Since you want to call it from the Web Application programmatically or your Application Module)
If you want to add another Validation, then you should follow the first answer.

cakephp - what is the difference between model and behavior?

I understand that the behavior is supposed to extend the model and add functionality to it, but in most cases the fat-model idea is making the behavior useless, isn't it?
And, even preferred, ignore my first paragraph and just answer - please - the question in the title, and add an example to make it clear
thanks
A behavior is where you extract code that doesn't really belong in one specific models domain. Kind of like, helper functions, or a mixin/module (if you're familiar with that pattern from other programming languages).
If you're familiar with CakePHP helpers and components, you can look at it like this. A behavior is to Model as Helper is to View as Component is to Controller. Basically a set of functionality that will be used across multiple models.
Let's say you want to implement soft delete on all the models in your application. (Soft delete meaning, you don't actually delete the record, you just mark it as deleted). You wouldn't want to put the same soft delete code into every model. That's not very DRY! Instead you use a behavior to abstract out the functionality like so.
What we're trying to do is instead of delete the record, update the deleted on column with the current date (it will work the same way as created, modified). Then we'll change the find method to only retrieve records that aren't deleted.
// models/behaviors/soft_deletable.php
class SoftDeletableBehavior extends ModelBehavior {
function setup(&$Model, $settings = array()) {
// do any setup here
}
// override the delete function (behavior methods that override model methods take precedence)
function delete(&$Model, $id = null) {
$Model->id = $id;
// save the deleted field with current date-time
if ($Model->saveField('deleted', date('Y-m-d H:i:s'))) {
return true;
}
return false;
}
function beforeFind(&$Model, $query) {
// only include records that have null deleted columns
$query['conditions']["{$Model->alias}.deleted <>"] = '';
return $query;
}
}
Then in your model
Class User extends AppModel {
public $actsAs = array('SoftDeletable');
}
And from your controller, you can call all of our behavior methods on your model
Class UsersControllers extends AppController {
function someFunction() {
$this->User->delete(1); // soft deletes user with id of 1
$this->User->find('all'); // this will not exclude user with an id of 1
}
}
I hope this helps you.
Behaviors can be shared between Models. Typically a Behavior contains abstract code that can be applied to any model.
While you could of course write this for a single Model specifically, you would have to write it again for another Model. By abstracting it to be shared, you've created a Behavior.
In CakePHP a Behavior to a Model is the same relationship as a Component to a Controller or a Helper to a View.
An example of a Core Behavior in CakePHP is Containable. This allows you to have finer control over the relationships used in by find().
basically behaviors are used to make your application dry! and code reuse...
Check this link... it gives you simple tagging behavior which you can use in your post model

How do I associate a database table with a model in ATK?

I have legacy code which stores temporary data in the context. I would like to store this in the DB using the following model:
class Model_MyModel extends Model_Table {
function init(){
parent::init();
$this->addField('myString');
}
}
I can access the data from within the legacy Controller thus:
class Controller_LegacyController extends Controller {
$myString = $this->api->recall("legacyString");
}
But I can't see how to tie everything together (all the examples use a Form to link to the DB)
Thanks for your help,
Greg.
I find your question and code a bit confusing, but I'll try to help.
You don't need controller to be able to use your model. When calling $form->setModel() it automatically pick the right controller for you.
$page->add('MVCForm')->setModel('MyModel');
When you want to send data back into data-base, you should call $form->update(). There is a View you can use, which will do that for you called: FormAndSave
$page->add('FormAndSave')->setModel('MyModel'); // will also save data back to database.
If you load data from database, you need to call loadData() on the model. Your final code might look like this (stickyGET ensures that it pass get argument inside form submit handler):
$this->api->stickyGET('id');
$page->add('FormAndSave')->setModel('MyModel')->loadData($_GET['id']);
method recall() deals with sessions, so it seems as if you are reading data from the session. If you intend that and you want to see value of your session variable in the form, then this will do it:
$form->set('myfield',$this->api->recall('legacyString'));
I hope this will give you some hints on how to continue. Look through more samples, there are lots of them on http://agiletoolkit.org

Count number of posts in cakephp

I'm trying to create a menu in cake php where I can also know how many articles are inside the section, should I use a manual query, or does exist some existing method to do it?
My site menu:
- Works (12)
- Photos (35)
- Stuff (7)
- Contacts
My problem is also I didn't get how I can access to data like this for every view, this should be a main menu, so I should use this in every view but If i put it in default.ctp, every model deosn't exist, because I cannot access it from a view.
Does exist some page which talks about this?
Since those are separate models that are not related to each other, you'll need to do a manual count.
$this->Model->find('count');
EDIT
Ok, so looks like you are talking about different models.
If this is used in a menu, that means it will be shown in all pages.
You have two ways of doing this.
You can do it by having an AppController for you application. Basically, you can put this code in the beforeRender method so it runs everytime your a request is rendered
function beforeRender() {
App::import('Model', array('Work', 'Photo', 'Stuff'));
$work = new Work();
$workCount = $work->find('count');
//do the same for the other
$this->set('workCount', $workCount);
}
Have a look at this for more details on callbacks : http://book.cakephp.org/view/977/Controller-Methods#Callbacks-984
Secondly, you can do this via a helper. You can put the same code (that is inside the bforeRender) into a helper, and you can call the helper method.
You can look here for more info on creating a helper : http://book.cakephp.org/view/1097/Creating-Helpers
The CounterCache behavior will help you out:
http://book.cakephp.org/view/1033/counterCache-Cache-your-count

Resources