ID from bean of a relate field

I have a module which has a relate field. On this moudle i have a before save hook and trying to get a bean of the related field. This fild is called “house_type”

The code i have is:


<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');


class CreatePlotAutofill
{

 function main ($bean, $event, $arguments)
 {
		
	//load house type bean from id stored in the house type related field. 
   	$HouseTypebean = BeanFactory::getBean('CoMO_House_Types', $bean->house_type);
	$bean->floor_price= $bean->house_type->floor_price;
	
 }
}

But the contents of ‘$bean->house_type’ is the name of the related field, not the record id. I was thinking of using SQL to get the ID, but as this is before save, the values will not be in the database yet.

Any help would be great.

Cheers

Version 7.10.7

Sugar Version 6.5.25 (Build 344)

I have also tried connecting a many to one realationship with the house_type module and call realted beans using this code

<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

/**
*  before save hook to update total price on house types
*/
class CreatePlotAutofill
{

 function main ($bean, $event, $arguments)
 {
	//$bean->total_price = floatval($bean->wall_price) + floatval($bean->floor_price);
	

   if ($bean->load_relationship('como_plots_como_house_types')) {

               //Fetch related beans

               $relatedBeans = $bean->como_plots_como_house_types->getBeans();
			   
  

				$bean->name = $relatedBeans->name;    //this is line 25
				
           }


   
 }
}

But geting this error


[Wed Nov 14 17:27:29.329943 2018] [:error] [pid 1165] [client 192.168.223.1:51075] PHP Notice:  Trying to get property of non-object in /var/www/html/custom/modules/CoMO_Plots/CreatePlotAutofill.php on line 25, 

So i am assuming the returned bean is empty.

So sticking with the second method. If a do a var_dump of $relatedBeans. i can see the data i want in there, but no idea how to access it. i have attached the output as a file as it is alot!

maybe


$bean->name = $relatedBeans->?????->name

Any advice would be great

Ok, so i have done it,

I needed to add

$relatedBeans = reset($relatedBeans);

to select the first element in the array. As there should only be one related module as the realationship is many to one, I think this is ok.

If any one has suggestions on a cleaner way, I would be interested.

Cheers