Call to a member function getSchemaCollection() on null


#1

# Call to a member function getSchemaCollection() on null

getting this error. any suggestions?

Thanks

Regards
Anuj


#2

Please share your code. Without seeing your code nobody can tell you what is the problem.


#3

Here is the code:

function getRateExpiryWarnings($pastDayCount){
	
	
	
	$dayInHours = null;
	$reservationGraceHours = null;
	
	//Set the Current Date
	$currentDate = Date('Y-m-d');
	//Set the Cutoff Date as X amount of days (parameter value)
	$cuttOffDate = Date('Y-m-d', strtotime(Date("Y-m-d", strtotime($currentDate)). "$pastDayCount days"));
	
	//Obtain the data
	$results = $this->find('all', array(
		'fields' => array(
			'RatePeriod.id', 'RatePeriod.fromdate', 'RatePeriod.todate',
			'RatePeriod.enddate', 'RatePeriod.expirywarning', 'RatePeriod.rate_detail_id'			
		),
		'conditions' => array('OR' => array(
				'RatePeriod.todate >=' => $cuttOffDate
				//'RatePeriod.enddate >=' => $cuttOffDate
			)
		),
		'recursive' => 1,
		'cacher' => true,
		'link' => array(
			'RateDetail' => array(
				'fields' => array(
					'RateDetail.name'
				)
			)
		)		
	));
	//Loop through data
	$idx = 0;
	
	if(!empty($results)){
		$this->Users = new UsersTable();
		$dayInHours = $this->Users->getPreferenceValue('day-in-hours');
		$reservationGraceHours = $this->Users->getPreferenceValue('reservation-grace-hours');
		
		foreach ($results as $result) {
			
			//Get the End Date from current array
			$resultEndDate = $result['RatePeriod']['todate'];
			//Obtain the difference in days
			$dayCount = $this->getDayCount($currentDate, $resultEndDate, $dayInHours, false, false, $reservationGraceHours);
			//Get the Warning Days from current array
			$warningDays = $result['RatePeriod']['expirywarning'];
			
			//Ensure either date difference is less than or equal to specified amount of past days
			//or specified amount of past days is less than or equal to the date difference
			if($dayCount <= 21 || $dayCount <= $warningDays){
				//Add the "Days Left" amount to current array
				$results[$idx]['RatePeriod']['daysleft'] = $dayCount;
			}
			else{
				//Remove current array from the results
				unset($results[$idx]);
			}
			
			$idx++;
		}
	
	}
	
	//Return the results
	return $results;
}

#4

I have debug my code and I have find that connection is not established while accessing table function. Its giving null when I have debug in cakphp\vendor\cakephp\cakephp\src\Datasource\ConnectionManager.php file.
But if I am trying to access database from controller. It is working fine.

Any suggestions?

Regards
Anuj


#5

When you are creating a new object (table) inside another table object you can indicate the current connection to create the Table object.
For example:

Here:

$this->User = new UsersTable();

if you are in the UsersTable you don’t need to do that. But, if you are out from UsersTable you need do something like this:

$userTable = new UsersTable(['connection' => $this->connection()]);
$userTable-> getPreferenceValue('day-in-hours');

Bye.


#6

is this CakePHP 2 code?


#7

I don’t know. I just know that it worked for me.


#8

It looks like CakePHP 2 code. It can work with CakePHP 3, but you can not be sure how long. So it is better to refactor it for CakePHP 3


#9

Hi @carbonera

I have tried the same but it is not working… any other solution :frowning:

I have find one more link related to this problem. I have tried that but still not working but I am sharing here for knowledge.


#10

Hi @anujg1

wich database are you using ? If is it postgresql, remember to see if the owner it’s the same of the other tables.
Could you give an var_dump from the variables $this->connection() and usersTable(after the object creation) and post for us ?