Call to a member function on null ZF2 Doctrine ORM 2 - php

$section = $objectManager->find('OEC\Entity\Section', $sectionId );
$class = $objectManager->find('OEC\Entity\Classes', $section->getClassId() );
$cycle = $objectManager->find('OEC\Entity\Cycle', $class->getCycleId() );
$branch = $objectManager->find('OEC\Entity\Branch', $cycle->getBranchId() );
$sectionArr = $class->getClassName()." ".$section->getSectionName()." - ". $branch->getBranchName()." ".$cycle->getCycleName();
$objectManager->close();
I am getting Call to a member function getCycleId() on null, although if I print_r($variable);exit; after each variable I obtain a result up until the end, only when I remove it it gives me the error. What can be the solution?

Try to debug it with something like:
$section = $objectManager->find('OEC\Entity\Section', $sectionId );
$class = $objectManager->find('OEC\Entity\Classes', $section->getClassId() );
if ($class === null) {
/* add debug info here*/
var_dump('ID of class was '.$section->getClassId());
var_dump('ID of section was '.$sectionId);
var_dump((new \Exception())->getTraceAsString());
die();
}
$cycle = $objectManager->find('OEC\Entity\Cycle', $class->getCycleId() );
$branch = $objectManager->find('OEC\Entity\Branch', $cycle->getBranchId() );
$sectionArr = $class->getClassName()." ".$section->getSectionName()." - ". $branch->getBranchName()." ".$cycle->getCycleName();
$objectManager->close();
Like I said in my comment, maybe the code is runnig multiple times.

Related

Laravel conditional statement with each() result error

Partly confused (still) because the variable $investment_type resulting of "Creating default object from empty value" when I follow this previous question of mine Laravel list() with each() function error with deprecated function.
This is the original code.
$assetsData = ClientPropertyManagement::find($assets_id);
$investmentType = Input::get('investmenttype'.$assets_id);
$legalname = Input::get('legalname'.$assets_id);
$ownership = Input::get('ownership'.$assets_id);
$tic = Input::get('tic'.$assets_id);
$entity_id = Input::get('entity_id'.$assets_id);
foreach($investmentType as $investment_type) {
list($key,$value) = each($legalname);
list($key,$valueOwner) = each($ownership);
list($key,$valueTic) = each($tic);
list($key,$valueEntityId) = each($entity_id);
if($valueEntityId == 0) {
$assetEntity = new ClientEntityManagement;
$assetEntity->property_id = $assetsData->property_id;
$assetEntity->client_id = $id;
} else {
$assetEntity = ClientEntityManagement::find($valueEntityId);
}
$assetEntity->investment_type = $investment_type;
$assetEntity->entity_name = $value;
$assetEntity->ownership = $valueOwner;
$assetEntity->ticnum = $valueTic;
$assetEntity->save();
}
Here's what I did in my code.
foreach( $investmentType as $key => $investment_type ) {
$assetEntity->investment_type = $investment_type;
$assetEntity->entity_name = $legalname[$key];
$assetEntity->ownership = $ownership[$key];
$assetEntity->ticnum = $tic[$key];
if ( $entity_id[$key] == 0 ) {
$assetEntity = new ClientEntityManagement;
$assetEntity->property_id = $assetsData->property_id;
$assetEntity->client_id = $id;
} else {
$assetEntity = ClientEntityManagement::find($investment_type);
}
$assetEntity->save();
}
The problem is that $assetEntity isn't created yet, and you are trying to use as an object. To solve that, you need to change the position where you instantiate the object and create the variable $assetEntity:
foreach( $investmentType as $key => $investment_type ) {
if ( $entity_id[$key] == 0 ) {
$assetEntity = new ClientEntityManagement;
$assetEntity->property_id = $assetsData->property_id;
$assetEntity->client_id = $id;
} else {
$assetEntity = ClientEntityManagement::find($investment_type);
}
$assetEntity->investment_type = $investment_type;
$assetEntity->entity_name = $legalname[$key];
$assetEntity->ownership = $ownership[$key];
$assetEntity->ticnum = $tic[$key];
$assetEntity->save();
}
That way $assetEntity will be created, and then you populate the other attributes.
Also, you may want to use the IoC Container and replace the new ClientEntityManagement with App::make('ClientEntityManagement'). Read more at https://laravel.com/docs/4.2/ioc

Trying to learn Laravel. How do I create a separate "post" function using this "get" codes?

ManagementController.php
public function getDuplicate(Request $request)
{
$data = \App\Chalet::find( \Crypt::decrypt($request->input('cid')) );
if ( $data->group_id == Auth::user()->group->group_id ) {
$cid = \Crypt::decrypt($request->input('cid'));
$mid = Extras::getMeta( 'chalet_details', '\App\ChaletMeta', $cid, 'chalet_id', true );
$sid = Extras::getMeta( 'chalet_status', '\App\ChaletMeta', $cid, 'chalet_id', true );
$ciid = Extras::getMeta( 'chalet_images', '\App\ChaletMeta', $cid, 'chalet_id', true );
$arid = Extras::getMeta( 'auto_release_excemption', '\App\ChaletMeta', $cid, 'chalet_id', true );
$rchalet = \App\Chalet::find($cid);
$dup_chalet = $rchalet->replicate();
$dup_chalet->unit_no = '';
$dup_chalet->status = 0;
$dup_chalet->visibility = 1;
$dup_chalet->save();
$nCID = $dup_chalet->chalet_id;
$rmchalet = \App\ChaletMeta::find($mid);
$dupM_chalet = $rmchalet->replicate();
$dupM_chalet->chalet_id = $dup_chalet->chalet_id;
$dupM_chalet->save();
$archalet = \App\ChaletMeta::find($arid);
if ( $archalet ) {
$dupA_chalet = $archalet->replicate();
$dupA_chalet->chalet_id = $dup_chalet->chalet_id;
$dupA_chalet->save();
}
$schalet = \App\ChaletMeta::find($sid);
$dupSChalet = $schalet->replicate();
$dupSChalet->chalet_id = $dup_chalet->chalet_id;
$dupSChalet->save();
$cichalet = \App\ChaletMeta::find($ciid);
$dupCIChalet = $cichalet->replicate();
$dupCIChalet->chalet_id = $dup_chalet->chalet_id;
$dupCIChalet->save();
return Redirect::to(Auth::user()->group->alias.'/app/echalet/view/?cid=' . \Crypt::encrypt($nCID) .'&action=view')->with('message', Extras::alert('success', 'Successful Duplicated Chalet! Please enter Unit #.'))->with('duplicate', true);
} else {
return Redirect::to( Auth::user()->group->alias.'/app/echalet' )->with('message', Extras::alert( 'error', "Something went wrong, Didn't managed to duplicate chalet" ));
}
}
This "get" function creates a duplicate of an item after clicking the button that says "duplicate". I wish you guys can help me point out which part of the code should I remove so I can make a separate "post" function.
This code replicates the ChatletMeta model and returns a message. If you want to modify the method(GET/POST) this function can be called, you need to modify the route belonging to this Controller/action.
Also, you can remove the Model replications.

Call PHP function variable from separate file

I have a class function that calls JSON from a URL. That function then sets a list of variables from the results of the JSON.
What I am attempting to do is call back one of those strings from inside of another file (index.html). I do not receive any errors back, however the results are blank.
I'm sure it's not due to the json command because it works properly when not placed inside of a class/function. To be sure I attempted to add $somestring4 = 'this is string 4' into the ClassFile.php and call it - it was also blank.
Here is my code...
ClassFile.php
<?php
class newClass {
Function newFunction(){
$jsonFetched = file_get_contents('http://url.com/file.json'); //<== MISSING SINGLE QUOTES ADDED FOR CODE-READABILITY...
$jsonQuery = json_decode($jsonFetched);
$someString1 = $jsonQuery->level1->level2->string1 ;
$someString2 = $jsonQuery->level1->level2->string2 ;
$someString3 = $jsonQuery->level1->level2->string3 ;
}
}
$foo = new newClass;
?>
Call from Index.html
<?php
include($sitePath . '/classes/ClassFile.php') ;
$refClass = new newClass();
$someString3 = $refClass->newFunction();
echo $someString3;
?>
Thanks for the help and sorry for the ignorance.
<?php
class newClass {
public $jsonFetched = '';
public $jsonQuery = '';
public $someString1 = '';
public $someString2 = '';
public $someString3 = '';
function newFunction(){
$jsonFetched = file_get_contents('http://url.com/file.json');
$jsonQuery = json_decode($jsonFetched);
$this->someString1 = $jsonQuery->level1->level2->string1 ;
$this->someString2 = $jsonQuery->level1->level2->string2 ;
$this->someString3 = $jsonQuery->level1->level2->string3 ;
}
}
?>
<?php
include($sitePath . '/classes/ClassFile.php') ;
$refClass = new newClass();
$refClass->newFunction();
echo $refClass->someString3;
?>

sqlite3 replacement for sqlite_has_more

First of thank you for your help.
The code piece "while (sqlite_has_more($dres))" is using sqlite2 and I need sqlite3. If there isn't a replacement for has_more is there another code I can use to still Find whether or not more rows are available?
F.Y.I. The server updated their stuff which included their sqlite and now I have to fix this last peice of code to get the schedule to populate and not give me this error.
Fatal error: Non-static method SQLite3::open() cannot be called statically in /home/server/public_html/current-list.php on line 57
$row_num = 0;
if ($dbh = SQLite3::open($sked_path))
{
$qsql = "SELECT rowid,* FROM sked ORDER BY sk_dow_num, sk_time_start, sk_time_end";
$dres = SQLite3::query($dbh, $qsql);
if (SQLite3::num_Rows($dres) > 0)
{
$last_dow = "";
$last_start = "0000";
$last_end = "0000";
while (sqlite_has_more($dres))
{
$ska = Sqlite3Result::fetchArray($dres, SQLITE3_ASSOC);
$rid = $ska['rowid'];
$dow = $ska['sk_dow_name'];
$start = $ska['sk_time_start'];
$end = $ska['sk_time_end'];
$title = preg_replace("/<br\s*\/*>/", " ", $ska['sk_show_title']);
$show_dow = strtoupper($dow);
$show_start = strtoupper(formatTimeAmPm($start));
$show_end = strtoupper(formatTimeAmPm($end));
$show_style = "";
if (stristr($title, "Encore Show"))
$show_style = " class=\"$text_style\"";
Something like ...
<?php
$dbh = new SQLite3;
if ( !$dbh->open($sked_path) ) {
trigger_error('...error handling...', E_USER_ERROR);
}
else {
$dres = $dbh->query('
SELECT
rowid,*
FROM
sked
ORDER BY
sk_dow_num, sk_time_start, sk_time_end
');
if ( !$dres ) {
trigger_error('...error handling...', E_USER_ERROR);
}
else {
$ska = $dres->fetchArray(SQLITE3_ASSOC);
if ( !$ska ) {
onNoRecords();
}
else {
do {
doSomethingWithRowData($ska);
}
while( false!=($ska=$dres->fetchArray(SQLITE3_ASSOC)) );
}
}
}
(completely untested)

failure with find() function using PHP with Silex

Here is the failure message I get in Terminal running 'phpunit tests':
1) StylistTest::test_find
null does not match expected type "object".
/Users/evanbutler/Desktop/hairSalonApp/tests/StylistTest.php:163
Here's my test method:
function test_find()
{
//Arrange
$name = "Stylist Jane";
$id = 1;
$name2 = "Stylist Bob";
$id2 = 2;
$test_stylist = new Stylist($name, $id);
$test_stylist->save();
$test_stylist2 = new Stylist($name2, $id2);
$test_stylist2->save();
//Act
$result = Stylist::find($test_stylist->getId());
//Assert
$this->assertEquals($test_stylist, $result);
}
And here's my method:
static function find($search_id)
{
$found_stylist = null;
$stylists = Stylist::getAll();
foreach($stylists as $stylist) {
$stylist_id = $stylist->getId();
if ($stylist_id == $search_id) {
$found_styist = $stylist;
}
}
return $found_stylist;
}
Here's my getAll method:
static function getAll()
{
$returned_stylists = $GLOBALS['DB']->query("SELECT * FROM stylists;");
$stylists = array();
foreach($returned_stylists as $stylist) {
$name = $stylist['name'];
$id = $stylist['id'];
$new_stylist = new Stylist($name, $id);
array_push($stylists, $new_stylist);
}
return $stylists;
}
If you'd like to see all my files here's the link to the git repository:
https://github.com/evanb2/hairSalonApp.git
I've been staring at this for way too long and I'm totally stumped.
Change
$found_styist = $stylist;
to
$found_stylist = $stylist;
You need a better IDE man. Simple static analysis could tell you about the unused variable.

Categories