Why does Symfony look for "addWooklyExercise()" instead of "addWeeklyExercise()" - php

I changed, by client requirement, an entity textfield (weekly_exercise) to a 1:n relation. Everything works normally so far, but when trying to save the form Symfony looks out for a "changed" method name.
That's the error message I get
Neither the property "weekly_exercise" nor one of the methods "addWooklyExercise()"/"removeWooklyExercise()", "setWeeklyExercise()", "weeklyExercise()", "__set()" or "__call()" exist and have public access in class "XXX\CourseBundle\Entity\Course".
Of course "addWooklyExercise()"/"removeWooklyExercise()" don't exist. I could put them in and proxy to the real methods, but that would only be an ugly hack.
I've been looking through all my files and couldn't find a anything that could be responsible for this issue.
I'm on Symfony 2.5.7 as my client doesn't allow me to update!!
Files involved in the issue https://gist.github.com/mhauptma73
EDIT:
For some reason the method
public function addWooklyExercise(\BDA\CourseBundle\Entity\CourseWeeklyExercise $weeklyExercise)
{
$this->__initializer__ && $this->__initializer__->__invoke($this, 'addWooklyExercise', array($weeklyExercise));
return parent::addWooklyExercise($weeklyExercise);
}
is being added in the cache proxy. But there is also the correctly spelled method, before the misspelled one.

Follow the naming convention like when you add the property for oneToMany then it's plural and for add or remove method change it to singular.
protected $tags;
public function addTag(Tag $tag)
{
//...
}
public function removeTag(Tag $tag)
{
// ...
}
See more details read this doc:
http://symfony.com/doc/2.7/form/form_collections.html#form-collections-new-prototype

That looks like you have a typo somewhere, probably your Entity, your Form-Type or when outputting a form in a template. Try doing a search over the whole src/ directory for wookly and you will probably find it.

Related

Laravel model accessor with attribute name containing a number is ignored

In Laravel 5.7 in my database I have a property containing a number in its name - item_1_quality. When I create an accessor with method name using camel case it is ignored.
I have tried various combinations the most obvious of which was
public function getItem1QualityAttribute($value)
{
dd($value);
}
however, it does not work. I tried other possible combinations without success. I am properly calling the property as other accessors in the same model work fine. The problem seems to be related to the naming.
I have found a hacky but a working solution which someone might find helpful.
Add attribute to appends
protected $appends = ['product_1_quality'];
Create the following accessor:
public function getProduct1QualityAttribute()
{
$value = $this->attributes['product_1_quality']);
// do something with your $value and return.
}
The $value as argument will not work - access using the $attributes array.

Laravel 5 Eloquent custom constructor not working

I'm trying to get a custom constructor to work with a model extending an Eloquent model in Laravel 5.4
I already make sure to call the parent constructor, but it seems that nothing that I do takes any effect at all after that.
Here is my __construct function:
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->users();
}
And here is the users() method:
public function users()
{
$this->users = collect();
foreach($this->employees as $employee) {
$this->users = $this->users->push($employee->user);
}
$this->users = $this->users->unique();
}
In this example employee is a class that links a user to a store and also defines their jobs. However, it doesn't matter what I try to assign. I have also tried just assigning a garbage variable in the constructor with
$this->foo = 'bar';
or even trying to overwrite an attribute, such as
$this->name = 'foobar';
to no avail. I've also tried to simply switch the order of the code calling parent::__construct() before or after my code and nothing at all changes.
Any help would be greatly appreciated!
If changes in your child class are not showing up, even when you load garbage into it, it sounds like it to me that you might not be loading the right class into the context?
Perhaps check your use statements at the top to make sure you loaded the child class and not the parent. It would be easy not to notice, since they share the same interface, no syntax error would be called inherently.
If that's all OK, then I would check the logs for anything suspicious (e.g. error messages related to the function).
As well, it's very useful to use the dd() function along the execution chain to see the type and contents of your variables. You may find it being overridden at some unexpected point.
So, I'm an idiot, basically. The constructor functionality was working perfectly. My problem was I was trying to set attributes not on a new instance of a class, but after retrieving it, which worked out better by just using the getAttribute methods.
Thank you everyone for trying to help, but it was just me being dumb.

CakePHP $this Syntax

I've been trying to learn CakePhp for a while now but I still can't get alot of stuff. I've been reading a lot and watching videos. I just want to ask a very simple question.
I've been trying to mess with the bookmarks tutorial and i'm watching a video. In the video he baked a component called Validate. In the cmd he typed
bin/cake/bake component Validate
Then a ValidateComponent.php appeared in the component folder in the controller. Now he used the ValidateComponent.php by going to the BookmarksController and adding to the initialize method
$this->loadComponent('Validate');
I just want to ask where did the word validate come from? shouldn't it be ValidateComponent? and where does he get the loadComponent from? I've seen him using $this->method(); or $this->method('string', [array]); I just want to know how the syntax works and what each word means. Sorry for the long explanation. I'm really trying to learn and i'm really confused. thank you very much.
ValidateComponent.php
<?php
namespace App\Controller\Component;
use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;
/**
* Validate component
*/
class ValidateComponent extends Component
{
/**
* Default configuration.
*
* #var array
*/
protected $_defaultConfig = [];
public function validLimit($limit, $ default)
{
if (is_numeric($limit)){
return $limit;
}
return $default;
}
}
part of BookmarksController.php
public function initialize()
{
parent::initialize();
$this->loadComponent('Validate');
}
I can't seem to find where he got the word 'Validate'
Every controller in your application extends a base Controller Controller or AppController which extends Controller
Controller have bunch of methods, One of these methods is the loadComponent() (See Source)
public function loadComponent($name, array $config = [])
{
list(, $prop) = pluginSplit($name);
$this->{$prop} = $this->components()->load($name, $config);
}
Why Validate instead of ValidateComponent?
Short answer: suffix.
See predefined suffix in App class
CakePHP uses suffix to load classes, When you hit loadComponent() You go to ComponentRegistery class to Register the component, ComponentRegistery will call App class to load class. __loadClass()
Almost everything in CakePHP has a suffix, In your case ValidateComponent has the Component suffix.
return App::className($class, 'Controller/Component', 'Component'); (Source)
I hope this will make more sense to you
$this isn't specifically anything to-do with cake but part of PHP itself. In object oriented context $this refers simply to the current class.
$this->something refers to an object within the current scope. This could be within the current class or from an extends or use.
$this->something(); refers similarly to a method or function within the current scope.
If are using an IDE such as netbeans you can usually click through these references to see the object they refer to so for example if you do in fact use Netbeans you could ctrl-click on $this->loadComponent('Validate'); to see the actual function it refers to.
Regarding where does 'Validate' come from, it's a string you are passing to that object. On the other end it will be used in the function, probably in a switch or if statement to return something.
Eg:
Public function loadComponent($type){
If($type == 'Validation'){
//do something
}
}

Override default rest action in Yii2

I created rest api in yii2 for the users. I can access list of users like this "api/web/v1/users" but the problem is that it is giving the data of all the columns including password, I saw in yii2 documentation that it is internally calling "user/index" method, is there any way to override the index method like this?
class UserController extends ActiveController {
public $modelClass = 'common\models\User';
public function actionIndex(){
//return selected columns here of the user table
}
}
It still gives the list of all users with all columns that I don't want. Please help.
In your case, you must use fields() method and override this method. As Yii defines fields():
By overriding [[yii\base\Model::fields()|fields()]] and/or [[yii\base\Model::extraFields()|extraFields()]], you may specify what data, called fields, in the resource can be put into its array representation.You can override fields() to add, remove, rename or redefine fields
For example:
public function fields()
{
return [
'id','name','username'
];
}
Above method, tells yii that only show id,name,username fields. So, Password will never be sent to client.
In cases that you want only remove one or more specific fields, you can do like below:
public function fields()
{
$fields=parent::fields();
unset($fields['password']);
return $fields;
}
While the accepted answer does work and is an important method to know for basic control of what fields are shared via rest and general "object exporting" functions like Json::encode(), I feel it is important to also understand how to completely override an action like the OP references.
I've answered this question here: https://stackoverflow.com/a/50744982/3337682, and I feel it would be helpful, added information for the OP.
Hope this helps someone!
~ Cheers :)

How to clear or empty the Array Collection in doctrine symfony

I have the Array collection of objects like this
Class User
{
private $tasks;
}
How can i empty or clear the collection once user gets loaded from database.
When i query for user then Doctrine will lazy load the tasks in user object but i want to first clear those tasks
something like
$user->getTasks().empty()
First of all, I imagine your User entity's constructor looks something like this:
class User
{
public function __construct()
{
...
$this->tasks = new \Doctrine\Common\Collections\ArrayCollection();
...
}
}
If that's not correct so far, then stop reading, and correct me in the comments :)
Note that the ArrayCollection class was created by Doctrine. Symfony and most of its components are pretty good about documenting the classes. When you look up that class, you'll find:
https://www.doctrine-project.org/api/collections/latest/Doctrine/Common/Collections/ArrayCollection.html
(of course, make sure you're on the same version; otherwise, try to find the documentation for your version)
The documentation lists all the methods available to the ArrayCollection object. Among them: clear().
That said, adding a new method to the User class should work:
class User
{
public function clearTasks()
{
$this->getTasks()->clear();
}
}
Then, on the User object, just call:
$user->clearTasks();
(and don't forget to persist to the database!)

Categories