Yii2 error when using $this inside of a view generated with Gii - php

In my Yii2 application I generated some CRUD with Gii.
However inside of the view that was generated I have a fatal error thrown.
Fatal error: Uncaught Error: Using $this when not in object context in C:\xampp\htdocs\Yii2_UAH\basic\views\listings\index.php:13 Stack trace: #0 {main} thrown in C:\xampp\htdocs\Yii2_UAH\basic\views\listings\index.php on line 13
I'm not sure why this is as I used Gii to generate the CRUD for it.
I'm thinking it may be to do with XAMPP but I'm not too sure.
I've made sure the files look similar to another project that works fine and they do look exactly the same so I've no idea why this is not working.
The error in particular is with this line $this->title = 'Listings'; in my view, but this is all out of the box Yii with the CRUD generated by Gii.
My Controller also has the following out of the box code so there shouldn't be anything wrong from a code point of view:
public function actionIndex()
{
$searchModel = new ListingsSearch();
$dataProvider = $searchModel->search($this->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
If anyone could point me in the right direction I'd appreciate that

Related

MongoDB insertOne function not found

I'm trying to use MONGODB in my project and I just started using it when it has this problem. I created another directory and ran the files with no problems whatsoever. What is wrong with this? I keep getting Fatal error: Uncaught Error: Call to undefined method MongoDB\Database::insertOne() error message.
This is my code to the class called Database which is producing the error.
<?php
namespace auth;
include_once dirname(__DIR__) . "/config.php";
class Database
{
public function __construct(
private readonly string $dbname,
){
$this->run();
}
private function run(): void
{
$collection = (new \MongoDB\Client)->{$this->dbname};
$insertOneResult = $collection->insertOne([
'username' => 'admin',
'email' => 'admin#example.com',
'name' => 'Admin User',
]);
printf("Inserted %d document(s)\n", $insertOneResult->getInsertedCount());
var_dump($insertOneResult->getInsertedId());
}
}
and my test.php file just contained some lines of code
include_once "./assets/php/config.php";
$database = new \auth\Database("test->users");
I definitely loaded the class MongoDB and my config.php includes the vendor file of composer's.
This is the error in full.
Fatal error: Uncaught Error: Call to undefined method MongoDB\Database::insertOne() in C:\xampp\htdocs\PP\assets\php\Classes\Database.php:18
Stack trace:
#0 C:\xampp\htdocs\PP\assets\php\Classes\Database.php(11): auth\Database->run()
#1 C:\xampp\htdocs\PP\test.php(15): auth\Database->__construct('test->users')
#2 {main}
thrown in C:\xampp\htdocs\PP\assets\php\Classes\Database.php on line 18
PHP Fatal error: Uncaught Error: Call to undefined method MongoDB\Database::insertOne() in C:\xampp\htdocs\PPa\assets\php\Classes\Database.php:18
Stack trace:
#0 C:\xampp\htdocs\PP\assets\php\Classes\Database.php(11): auth\Database->run()
#1 C:\xampp\htdocs\PP\test.php(15): auth\Database->__construct('test->users')
#2 {main}
thrown in C:\xampp\htdocs\PP\assets\php\Classes\Database.php on line 18
Process finished with exit code 255
The config.phpfile that I have included
include_once dirname(__DIR__, 2) . "/vendor/autoload.php";
include_once "autoload.php";
Thanks for any help.
Ah yes answer to my question, I'm just well not thinking properly at the time of writing the code. The `MongoDB\Collection" class accepts two arguments from the user and one internally set. The user provided argument is the database name and collection name. Doing it with (new \MongoDB\Client)->{$this->dbname} is wrong because it provides just the database string and not the collection name.
I resolved this issue by changing how the structure looks of the collection. md is just the alias for MongoDB class as I do not want to type such a long string everytime.
$collection = (new md\Client)->$db->$collection_name;
However, I have no idea how mongoDB uses the db and collection_name as magic constants as I have looked through the entire MongoDB\Client file and found there is no code stating those "magic constants"? I would appreciate if someone has an answer to this.

Webtools and developer tools issue on Phalcon framework

I installed phalcon 3.0.1-14 on an Ubuntu 14.04 box. Also installed Phalcon DevTools (3.0.1).
Initially, I enabled the webtools and when I visit that page, some warnings appear all the time:
Cannot bind an instance to a static closure in /home/pish/vendor/phalcon/devtools/scripts/Phalcon/Web/Tools.php
Cannot bind an instance to a static closure in /home/pish/vendor/phalcon/devtools/scripts/Phalcon/Web/Tools/views/index.phtml
I just ignored them and tried to create a model out of an existing table in the database. When I clicked on "Generate" button
I get the following error:
Phalcon\Mvc\Dispatcher\Exception: ModelsController handler class cannot be loaded
and the model is not created. I tested creating a controller as well, but a similar error occurred and the controller
was not created either.
Finally, I created the model via the console phalcon model users and it was created successfully.
I noticed, though, that the validation function created by the developer tools doesn't work and causes the following
error when I try to create a user:
Catchable fatal error: Argument 1 passed to Phalcon\Mvc\Model::validate() must implement interface Phalcon\ValidationInterface, instance of Phalcon\Mvc\Model\Validator\Email given in...
My question is basically, is there something bad with the version of developer tools I installed that causes the problems
with the Webtools and the functions that are generated for models/controllers, etc.? Or I might have something wrong
in my system?
Cannot bind an instance to a static closure
https://github.com/phalcon/cphalcon/issues/11029
Catchable fatal error: Argument 1 passed to Phalcon\Mvc\Model::validate()
Fixed in the 3.0.x branch (will be released soon)
Regarding your second error message:
Catchable fatal error: Argument 1 passed to Phalcon\Mvc\Model::validate() must implement interface Phalcon\ValidationInterface, instance of Phalcon\Mvc\Model\Validator\Email given in...
Model validation has changed in Phalcon 3.0. In Phalcon v2 you had to do
public function validation()
{
$this->validate(
new Phalcon\Mvc\Model\Validator\Email(['field' => 'email']);
);
if ($this->validationHasFailed() == true) {
return false;
}
}
But the Phalcon\Mvc\Model\Validation is deprecated in v3 and you should use Phalcon\Validation instead. Just alter your code to the following:
public function validation()
{
$validator = new Validation();
$validator->add(
'email', //your field name
new Phalcon\Validation\Validator\Email([
'model' => $this,
'message' => 'Please enter a correct email address'
])
);
return $this->validate($validator);
}
Maybe the DevTools haven't updated this part yet, I am not sure.

Yii cache error: Trying to get property of non-object

I'm using Yii PHP Framework and MemCache for caching and when I open not cached page for the first time I see this error:
Live error error log off (linux):
Fatal error: Call to a member function getColumnNames() on a non-object in /home/cineshell/cineklik/framework/db/ar/CActiveFinder.php on line 385
Locally with error log (windows):
Trying to get property of non-object
C:\xampp\htdocs\appfolder\framework\yiilite.php(7125)
...
7123 public function getTableSchema()
7124 {
7125 return $this->getMetaData()->tableSchema;
7126 }
config/main:
'cache'=> array(
'class'=> 'CMemCache',
'servers'=>array(
array('host'=>'localhost', 'port'=>11211, 'weight'=>100),
),
),
Cache example:
$data = Yii::app()->cache->get( 'casheID' );
if($data===false)
{
$data = dataprovider, query... etc
}
echo $data;
So, what's error here ?, and could be something in server settings ?
someone told me use if isset but I'm not understand !
For CActiveDataProvider, please follow this guide.
http://www.yiiframework.com/wiki/233/using-cache-in-cactivedataprovider/
For alternative, you can use fragment cache the gridview or the partial view depending the needs. I've also struggeled with it and I found that query cache was the only working solution.

Magento: what did I break? "Fatal error: Call to a member function insert()" (version 1.4.1.1)

I was trying to implement some new functionality, decided not to, tried to roll back, and now am stuck with this error:
Fatal error: Call to a member function insert() on a non-object in /app/code/core/Mage/Core/Model/Mysql4/Resource.php on line 96
The functionality I mentioned seems simple enough (remove a couple links from the customer account sidebar), and I believe I've removed the 3 files and the edit to local.xml, but just for full view on things, here's exactly what i did.
Add'l info: I've got one custom module in place that I built for this site, and it was working prior to this glitch, but I'm wondering if something got screwed up in there maybe? (And I'm also thinking I might not have committed my last/working change, and now I'm in "doubt" territory about whether I screwed that up. It's a simple one though: 1 controller, 1 route.)
I've got no access to PHP error logs or the shell (exec() works great though), and the /var/system.log isn't adding anything more on my subsequent page-refreshes. And so this is where I'm stuck - Got any ideas on how to track this down? Thanks in advance!
Edit / here's the function that's throwing the error:
function setDbVersion($resName, $version)
{
$dbModuleInfo = array(
'code' => $resName,
'version' => $version,
);
if ($this->getDbVersion($resName)) {
self::$_versions[$resName] = $version;
$condition = $this->_write->quoteInto('code=?', $resName);
return $this->_write->update($this->_resTable, $dbModuleInfo, $condition);
}
else {
self::$_versions[$resName] = $version;
// this is line 96:
return $this->_write->insert($this->_resTable, $dbModuleInfo);
}
}

Testing a form with missing elements gives me an error with is ErrorController.php

I'm doing some testing to my login form but I notice that If I dont send all the expected parameters, I get this error:
Fatal error: Call to a member function hasResource() on a non-object in C:\demo\application\controllers\ErrorController.php on line 47
for example this test code gives me error:
public function testLoginPage ()
{
$this->request->setMethod('POST')->setPost(
array('username' => 'foobar');
$this->dispatch('/usuario/login');
}
But if I send all the elements everything works as expected:
public function testLoginPage ()
{
$this->request->setMethod('POST')->setPost(
array('username' => 'foobar','password' => 'secret');
$this->dispatch('/usuario/login');
}
Is this normal? I dont understard why I get an error on ErrorController.php, where is the connection?
(I thought that maybe is something that not loading, but why is it working when all elemnts are?)
Any help understanding this will be appreciated.
Thanks
Update:
I just change to an incorrect password for db database in the application.ini and that gives me the same error. Now I dont even think is the form but maybe some call at the bootstart that is depending to the Zend_Auth identity. But what means that Fatal error: Call to a member function hasResource() on a non-object? how to load that object?
Read the error message you get. You're calling hasResource() on a non-object in ErrorController.php. Your error controller is broken, but that's not really the issue here.
The issue is that there's an exception being thrown somewhere (possibly in your form, bootstrap, or anywhere really) which triggers the error controller. Fix or disable the error handler to get the exception message and stack trace to find out your problem.
To disable the error handler
$front = Zend_Controller_Front::getInstance();
$front->setParam('noErrorHandler', true);

Categories