src/SnooPHP/Model/Model.php
public function belongsTo($refClass, $forColumn = null)
	{
		// Get columns
		$refTable = $refClass::tableName();
		$forTable = static::tableName();
		$refColumn = $refClass::$idColumn;
		$forColumn = $forColumn ?: strtolower($refClass::modelName())."_id";

		$rows = Db::query("
			select R.*
			from $refTable as R, $forTable as F
			where R.$refColumn = F.$forColumn and F.$forColumn = :id
		", ["id" => $this->$forColumn], static::getDbName());

		if ($rows === false)
			return false;
		if (empty($rows))
			return null;
		
		$refModel = new $refClass;
		foreach ($rows[0] as $col => $val) $refModel->$col = $refModel->decodeValue($val, $col);
		return $refModel;
	}
public function belongsTo($refClass, $forColumn = null)
	{
		// Get columns
		$refTable = $refClass::tableName();
		$forTable = static::tableName();
		$refColumn = $refClass::$idColumn;
		$forColumn = $forColumn ?: strtolower($refClass::modelName())."_id";

		$rows = Db::query("
			select R.*
			from $refTable as R, $forTable as F
			where R.$refColumn = F.$forColumn and F.$forColumn = :id
		", ["id" => $this->$forColumn], static::getDbName());

		if ($rows === false)
			return false;
		if (empty($rows))
			return null;
		
		$refModel = new $refClass;
		foreach ($rows[0] as $col => $val) $refModel->$col = $refModel->decodeValue($val, $col);
		return $refModel;
	}
public function belongsTo ( $ refClass , $ forColumn = null ) { // Get columns
 $ refTable = $ refClass :: tableName ( ) ; $ forTable = static :: tableName ( ) ; $ refColumn = $ refClass :: $ idColumn ; $ forColumn = $ forColumn ? : strtolower ( $ refClass :: modelName ( ) ) . "_id" ; $ rows = Db :: query ( "
			select R.*
			from $refTable as R, $forTable as F
			where R.$refColumn = F.$forColumn and F.$forColumn = :id
		" , [ "id" => $ this -> $ forColumn ] , static :: getDbName ( ) ) ; if ( $ rows === false ) return false ; if ( empty ( $ rows ) ) return null ; $ refModel = new $ refClass ; foreach ( $ rows [ 0 ] as $ col => $ val ) $ refModel -> $ col = $ refModel -> decodeValue ( $ val , $ col ) ; return $ refModel ; }
One-to-one inverse relationship

@param string $refClass reference model class
@param string $forColumn foreign key if different from refTable_id

@return Model referenced model or false if fails
One - to - one inverse relationship