Declaration of Child method should be compatible to Parent Method - php

got this error on Yii2. I dont know what exactly the problem is. I just migrate my source code from Windows to Mac OS. I tried cloning my whole project still the error appears.
Declaration of common\models\Product::getAttributes() should be compatible with yii\base\Model::getAttributes($names = NULL, $except = Array)
These are the things i tried:
Clone the whole project to Mac OS. - error above exists.
Clone the whole project to another windows machine.-the project is running well.
I am using yii2 in this project.

As you can see from the error message, you overrided yii\base\Model getAttributes() method. common\models\Product is extended from yii\db\ActiveRecord and ActiveRecord is extended from yii\base\Model.
If you really want to override this method, list all parameters (see here), it's easier to do with help of IDE. And by the way this is PHP feature and has nothing to do with OS or Yii2.
If it is your custom method for another purposes, you need to rename it in order to resolve conflict.

Your method must accept the same parameters ($names = NULL, $except = Array)

Related

Yii2 $model->_attributes assignment does not work in new version

I inherited a project that was created with Yii2, ver. 2.0.4, with the task to update said project to a more current version of Yii2 (2.0.15) because of the incompatibility of the older one with PHP 7.2+.
I noticed that there is a lot of use of assigning arrays to a model:
$model->_attributes = $array;
With the new version this results in an exception
'yii\base\UnknownPropertyException' with message 'Setting unknown property: app\models\model::_attributes'
For the time being I created a workaround with the following function:
function customSetAttributes(&$model, $array) {
foreach($model->attributeLabels() as $model_key => $model_label) {
if(!isset($array[$model_key])) continue;
$model->$model_key = $array[$model_key];
}
}
Also, the getter function now has a similar issue.
What I would like to know:
Was this type of assignment never intended in the first place (and I just haven't found the previous developer's code that enables it)? I skimmed over the Yii2 changelog but didn't notice anything related.
Is there a way to "salvage" the previous behaviour so I don't have to replace each occurence with my workaround function?
ActiveRecord::$_attributes was always private and never should be used in this way. I guess that previous developer edited framework core files in vendor directory and make this property protected/public.
You may try to emulate this behavior by creating virtual attribute using getter and setter:
public function get_attributes() {
return $this->getAttributes();
}
public function set_attributes($values) {
$this->setAttributes($values, false);
}
But this will not always work and it is more like an ugly hack to make crappy code work. I strongly suggest to fix code to use setAttributes() instead of _attributes.
Also you should compare yii2 package from vendor directory with source from https://github.com/yiisoft/yii2-framework/releases/tag/2.0.4 - you may find more places where core was edited.

Laravel cannot find a Controller Method (does not exist)

I'm trying to implement a new method in a BoController called "deleteBooking", the method is defined:
public function deleteBooking($id){
$booking = Reservation::find($id);
if($booking && $booking->delete()){
try {
$email = Mail::to($booking->user_email)->send(new Cancel($booking));
} catch(\Exception $e){
Log::error($e->getMessage());
}
return redirect('admin/manager/home')->with('message','Réservation annulée!');
}
return redirect('admin/manager/home')->with('message','Réservation non annulée!');
}
But laravel at the endpoint says:
(1/1) BadMethodCallException
Method [deleteBooking] does not exist.
Other methods from the same class are linked to endpoints too, and work well.
Do you have any ideas please? Thank you.
I got it fixed, I've found another file called BoController, in another folder somehow and it was conflicting with the App\Http\Controllers one.
Thank you.
It's most likely that you have declared that function for some other request type other than the one you're trying to make. For example you put Route::post('some-method', 'BoController#deleteBooking'); but you need to put either Route::get(...) or Route::put(...) or Route::delete(...).
If it isn't that problem, then you probably misspelled it.
I have faced similar issue. Then I have figured out a issue pointed in composer install log, with following instance of log line:
Class App\Http\Controllers\BlogController located in ./app/Http/Controllers/BlogControllerOld.php does not comply with psr-4 autoloading standard. Skipping.
Based on that I have found that one of the file renamed with Old suffix was creating conflict with the main file. So here I have to chhoseone of the following solutions:
To delete the file created for backup.
Or just rename the class in duplicated file to BlogControllerOld.
So its a good idea to check for issues with composer install
It will highlight the conflicts that can be fixed using one of the method above.
Once fixed using specified methods above issue composer install to apply the fix and regenerate autoloader.

Yii2: How to force using fallback MessageFormatter method?

My website is with a hosting provider that has the MessageFormatter class available on the server (Linux, PHP 7.0.27) but it is an old ICU version (4.2.1) that doesn't support my message {number,plural,=0{# available} =1{# available} other{# available}} and gives the error:
Message pattern is invalid: Constructor failed
msgfmt_create: message formatter creation failed: U_ILLEGAL_CHARACTER
...because of the =1 and =2 notation.
I'm not able to make changes to the server so how can I force using the fallback method provided by Yii2 which works just fine?
There is this hacky way you can try.
Copy the yii\i18n\MessageFormatter code to a new file. Name it MessageFormatter.php and place somewhere in your application (but not in vendor folder).
In this new file change the format() method to:
public function format($pattern, $params, $language)
{
$this->_errorCode = 0;
$this->_errorMessage = '';
if ($params === []) {
return $pattern;
}
return $this->fallbackFormat($pattern, $params, $language);
}
Don't change anything else (including namespace).
Now let's use Yii mapping.
Find a place in your application when you can put code that will be run every time in bootstrapping phase. Good place for this is common/config/bootstrap.php if you are using "Advanced Template"-like project.
Add there this line:
Yii::$classMap['yii\i18n\MessageFormatter'] = 'path/to/your/MessageFormatter.php';
Obviously change the path to the one you've chosen. Now Yii autoloader will load this class from your file instead of the original Yii vendor folder (as mentioned in Class Autoloading section of the Guide).
In the modified file MessageFormatter method presence of intl library is never checked so fallback is used as default.
The downside of this trick is that you need to update manually your file every time original Yii file is changed (so almost every time you upgrade Yii version).
Another approach is to configure I18N component in your application to use your custom MessageFormatter where you can extend the original file and just override format() method inside without modifying class map.

Cakephp 3 Interacive Component Action

I change with cakephp 3.2 to a new server and this server is running php 7 instead of 5.4. Now I have an issue with my interactive way of calling an component and the needed action. What I was using is the following:
$data[$csvKnowField->field_number] = $this->Replace->$csvKnowField['imports_mapping']['component_action']($data[$csvKnowField->field_number]);
This is giving the error: Function name must be a string. As far as I can see the problem is coming from the action called because if I change it to:
$data[$csvKnowField->field_number] = $this->Replace->replaceComma($data[$csvKnowField->field_number]);
everything works fine. The variable $csvKnowField['imports_mapping']['component_action'] is holding an interactive value so the different actions can be called in the Replace component, so it would be great to keep it that way.
Is there somebody how knows an solution so the the value in the variable can be used as an interactive way?

CakePHP Facebook integration tutorial "Fatal error: Call to a member function share() on a non-object" [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Call to a member function on a non-object
I'm doing this tutorial here:
http://tv.cakephp.org/video/webtechnick/2011/01/12/nick_baker_--_facebook_integration_with_cakephp
I baked a new project with cake bake facebook_app and set the configuration database file to the correct settings and the default cakePHP screen showed that tmp directory was writable, DB setup was good, etc.
I downloaded the CakePHP plugin by WebTechNick here: https://github.com/webtechnick/CakePHP-Facebook-Plugin, and filled out app information (app_secret, app_id, etc), adding it to facebook_app/config/facebook.php
Changed facebook_app/app_controller.php:
class AppController extends Controller {
var $name = 'Facebook';
var $helpers = array('Session', 'Facebook.Facebook');
}
Then just exactly as in the tutorial `facebook_app/views/pages/home.ctp':
<h1>Facebook_App</h1>
<?php $this->Facebook->share(); ?>
returning the error message:
Undefined property: View::$Facebook
I realize that means PHP didn't recognize Facebook as an object. But I installed the plugin!
Also, it seems not MVCish to have something like $this->Facebook->share(); in a view (home.ctp). However, this is exactly how WebTechNick does it in his tutorial (I followed it exactly 3x) and it does not work for me. I'm a complete noob at cakePHP (although I've read the entire documentation) and I'm just trying to learn and understand through examples.
:) To be fair, it's PHP - you didn't install anything. Or if you prefer, "install" != "invoke." PHP is really amazingly easy to debug. I mean, it tells you exactly what's wrong:
Like turning to a channel that's not on the air, the error your getting means that the object you're calling doesn't actually exist, at least not in the scope you're trying to invoke it.
Is that your IDE? Is it set up for your Cake app? Are you sure the instructions were to set your AppController's $name to 'Facebook' instead of $name = Facebook_App in your AppController? It looks like you either replaced your actual app's AppController with the plugin files instead of putting them in the proper directory, or the plugin is not deferring / calling / extending / returning to the application the way it's supposed to. Knee jerk -> typo, naming conflict, path problem, permissions.
Cake's not even rendering. I can tell because your screenshot would show that error with the styled Cake errors. That tells you it's erroring before AppController class makes it to View class.
Create an instance of the Facebook object statically in the view and see what happens. Then, what does
function beforeFilter() {
parent::__construct() ?
}
get you? Anything? What about debug(), var_dump, the object functions will also shed light on what's happening. So will your logfiles.
Btw, if you don't use them already: Firefox + FirePHP + Xdebug = made of win.
I was having this problem and I found that the plugin I was using was for CakePHP 1.3 and I was using Cake 2.0. I found the BETA branch for the upgraded Cake 2.0 and it worked perfect.
Here is the Cake 2.0 BETA Branch

Categories