Call to a member function getSchemaCollection() on null

# Call to a member function getSchemaCollection() on null

getting this error. any suggestions?

Thanks

Regards
Anuj

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

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;
}

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

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.

is this CakePHP 2 code?

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

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

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.

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 ?

I have used below syntax in my CakePHP3 project and it’s working fine.

use Cake\Datasource\ConnectionManager; /* add this line before your controller class definition */
    $db = ConnectionManager::get ( 'default' );
    $collection = $db->getSchemaCollection ();

I get the same message when I do a find () inside tablemodel.

// FilesTable.php
class FilesTable extends Table
{

    // others public functions

    public function deleteFiles($params)
    {
        $files = $this->find()->all(); // error!!!
        (...)
    }
}

worked with:

    $files = TableRegistry::getTableLocator()->get('FileManager.Files');
    $result = $files->find();
1 Like

I don’t see any reason why your original code wouldn’t work. Is that exactly what you had there? Is it for sure that particular line you got the error on? What was the error message?