Everything until now worked perfectly. I'm on page: http://framework.zend.com/manual/current/en/in-depth-guide/understanding-routing.html.
On this page I had to modify 3 files:
-module.config.php
-detail.phtml
-ListController.php
I get the following error:
Post Details
Post Title
Fatal error: Call to a member function getTitle() on null in C:\Program Files\xampp\htdocs\path\to\zf2-tutorial\module\Blog\view\blog\list\detail.phtml on line 6
I didn't paste the code, because it's the same from the link. Can you guys help me figure out my problem?
public function detailAction()
{
$id = $this->params()->fromRoute('id');
try {
$post = $this->postService->findPost($id);
} catch (\InvalidArgumentException $ex) {
return $this->redirect()->toRoute('blog');
}
return new ViewModel(array(
'post' => $post
));
}
Thanks for the update. Now that I see where you are in the tutorial I think you have a problem in the Mapper. See the previous page and chapter Finishing the Mapper
If your mapper cannot find an article it should throw an error as seen in that code example on line 63. Obviously your mapper returns null which causes the error you see Call to a member function getTitle() on null. Because null is not an object after all and doesn't have a getTitle() function.
So have a look at the ZendDbSqlMapper class and the find($id) method and make sure it throws an error if an id isn't found.
Related
People I'm having problem in time to make the edit with the doctrine.
the following error occurs: Fatal error: Call to a member function persist() on a non-object in
below is my code:
public function editAction()
{
$id = $this->params()->fromRoute('id', 0);
$user_update = $this->getEntityManager()->find('CodeDemo\Entity\User', $id);
$user_update->setName('User example');
$user_update->setEmail('user#example.com');
$user_update->setDate(new \DateTime('02/02/02'));
$this->getEntityManager()->persist($user_update);
$this->getEntityManager()->flush();
}
Thank you!!
I solved the problem. Was that the entity manager was null , so giving fatal error!
Inherited a piece of code where an update to a plugin has started causing this error
Fatal error: Call to a member function getId() on a non-object in...
Where the line in question is $shippingId = $order->getShippingAddress()->getId();
The update to the surrounding code, means that this function returning nothing is fine and the rest of the code can function with $shippingId not existing
What I need is a method of saying
if ($shippingId = $order->getShippingAddress()->getId() WORKS)
do_these_things()
and then instead of error'ing just carries on if not
I would do this:
if (isset($order->getShippingAddress()) && is_object($order->getShippingAddress()) ) {
/* It is an object and it isn't null */
} else {
/* It doesn't */
}
isset() checks if the expression is not null and has a value (to avoid null-pointing) and is_object() checks wether such expression is an object.
You can use the method mentioned by arielnmz or you could use custom exception handler.
try{
if ($shippingId = $order->getShippingAddress()->getId() WORKS)
do_these_things();
}
catch(Exception e){
//Echo out the error msg or save it on your error log stash. up to you
$error_msg=$e->getMessage();
}
//your regular codes
Using this way, if there is an error within the try block, the error is caught on the catch block and your code will carry on the normal execution pattern.
When I use the built in PDO layer in TinyMVC to request records from a table, it returns the following error:
Array
Error: 0
Message: Unknown file 'register_view.php'
File:{redacted}tinymvc\sysfiles\plugins\tinymvc_view.php
Line: 125
When I however try just returning the same variable without a connection it displays it fine. The error it is raising is in the view layer because it cannot get the parameter passed to it. However I see no reason why it is doing that considering the query is running ok.
Here is the code from the view method:
<body>
<h1>Hello <?=$fname?></h1>
<p>Hello World</p>
And the code from the controller:
function index(){
$this->load->model('User_Model','user');
$this->view->assign('title','Manage your Peacock account');
$this->view->assign('fname', $this->user->fname());
$this->view->display('user_view');
}
Finally the code from the model:
public function __get($property) {
if (property_exists($this, $property)) {
/**
* Retrieve the data from the database
*/
$this->$property = $this->db->query_one('select '.$this->getTable($property).' from users where id=?',array('1'));
return $this->$property;
}
}
Any help will be appreciated
TinyMVC has the unfortunate trait of declaring an "Unknown file" error whenever a view with a fatal error is "display"ed. The reason is simple: the include code for the view is in /tinymvc/sysfiles/plugins/tinymvc_view.php in the last function - "_view()" in a "try-catch" block:
try {
include($_tmvc_filepath);
} catch (Exception $e) {
throw new Exception("Unknown file '$_tmvc_filepath'");
}
so ANY fatal error raised by the view automatically throws the "Unknown file" error.
If you want to see the real error, comment all the above lines and add the line:
require($_tmvc_filepath);
instead. This will anyways raise a fatal error if the file really isn't found, and will show you the real error that is in the view.
Notice: Undefined variable: entryMessage in /var/www/Employee/application/models/EmployeeMapper.php on line 34 Fatal error: Call to a member function setEmployeeId() on a non-object in /var/www/Employee/application/models/EmployeeMapper.php on line 34
This is the error i am getting i try to display the entered fields , I checked the database and the fields are getting saved , I am posting the Employee Mapper .Please check the code and tell a solution , thanks in advance
class Application_Model_EmployeeMapper
{
protected $_dbTable;
public function setDbTable($dbTable)
{
if (is_string($dbTable)) {
$dbTable = new $dbTable();
}
if (!$dbTable instanceof Zend_Db_Table_Abstract) {
throw new Exception('Invalid table data gateway provided');
}
$this->_dbTable = $dbTable;
return $this;
}
public function getDbTable()
{
if (null === $this->_dbTable) {
$this->setDbTable('Application_Model_DbTable_Employee');
}
return $this->_dbTable;
}
public function fetchAll()
{
$table = $this->getDbTable();
$resultSet = $table->fetchAll($table->select()->order('EMPLOYEE_ID'));
$entries = array();
foreach ($resultSet as $row) {
$entry = new Application_Model_Employee();
$entryMessage->setEmployeeId($this->$row->EMPLOYEE_ID)
->setFirstName($row->FIRST_NAME)
->setLastName($row->LAST_NAME)
->setEmail($row->EMAIL)
->setPhoneNumber($row->PHONE_NUMBER)
->setHireDate($row->HIRE_DATE)
->setJobId($row->JOB_ID)
->setSalary($row->SALARY);
$entries[] = $entry;
}
return $entries;
}
public function save(Application_Model_Employee $employee)
{
$data = array(
'EMAIL' => $employee->getEmail(),
'FIRST_NAME' => $employee->getFirstName(),
'LAST_NAME' => $employee->getLastName(),
'PHONE_NUMBER' => $employee->getPhoneNumber(),
'HIRE_DATE' => $employee->getHireDate(),
'JOB_ID' => $employee->getJobId(),
'SALARY' => $employee->getSalary(),
);
Error you're getting is very clear, you should always read and try to understand the error message that you get, it gives you solution to most of the common problems. So always make a habit of reading and understanding the error message you get. Here,
Notice: Undefined variable: entryMessage in /var/www/Employee/application/models/EmployeeMapper.php on line 34
Notice says, undefined variable; which means that your variable $entryMessage is not defined. And the second part of the error that you are getting:
Fatal error: Call to a member function setEmployeeId() on a non-object in /var/www/Employee/application/models/EmployeeMapper.php on line 34
is very obvious since you are trying to call setEmployeeId() function from $entryMessage which has not been defined, yet you are assuming it to be an object and using it as an object.
I am assuming that $entryMessage you are using should have been $entry
These are quite simple and are not suitable to be asked in SO actually.
Once again, my suggestion to you are,
Always read what the error message says, it is what you are supposed to be using for debugging any bugs in the code
If you are new to programming you should perhaps use IDE's (netbeans,
eclipse, etc..) which actually shows the usage of variables and
function and syntax error in your code.
Always do your part of research and ask question only if not found or successful, and always show what you attempted. Others are there to help you but not to solve everything for you. :)
I'm calling a method that I know could cause an error and I'm trying to handle the error by wrapping the code in a try/catch statement...
class TestController extends Zend_Controller_Action
{
public function init()
{
// Anything here happens BEFORE the View has rendered
}
public function indexAction()
{
// Anything `echo`ed here is added to the end of the View
$model = new Application_Model_Testing('Mark', 31);
$this->view->sentence = $model->test();
$this->loadDataWhichCouldCauseError();
$this->loadView($model); // this method 'forwards' the Action onto another Controller
}
private function loadDataWhichCouldCauseError()
{
try {
$test = new Application_Model_NonExistent();
} catch (Exception $e) {
echo 'Handle the error';
}
}
private function loadView($model)
{
// Let's pretend we have loads of Models that require different Views
switch (get_class($model)) {
case 'Application_Model_Testing':
// Controller's have a `_forward` method to pass the Action onto another Controller
// The following line forwards to an `indexAction` within the `BlahController`
// It also passes some data onto the `BlahController`
$this->_forward('index', 'blah', null, array('data' => 'some data'));
break;
}
}
}
...but the problem I have is that the error isn't being handled. When viewing the application I get the following error...
( ! ) Fatal error: Class 'Application_Model_NonExistent' not found in /Library/WebServer/Documents/ZendTest/application/controllers/TestController.php on line 23
Can any one explain why this is happening and how I can get it to work?
Thanks
use
if (class_exists('Application_Model_NonExistent')) {
$test = new Application_Model_NonExistent;
} else {
echo 'class not found.';
}
like #prodigitalson said you can't catch that fatal error.
An error and an exception are not the same thing. Exceptions are thrown and meant to be caught, where errors are generally unrecoverable and triggered with http://www.php.net/manual/en/function.trigger-error.php
PHP: exceptions vs errors?
Can I try/catch a warning?
If you need to do some cleanup because of an error, you can use http://www.php.net/manual/en/function.set-error-handler.php
Thats not an exception, thats a FATAL error meaning you cant catch it like that. By definition a FATAL should not be recoverable.
Exception and Error are different things. There is an Exception class, which you are using and that $e is it's object.
You want to handle errors, check error handling in php-zend framework. But here, this is a Fatal error, you must rectify it, can not be handled.