sai_rama
(sai rama)
May 3, 2011, 9:45am
1
hi all,
i have 2 tables ChargesDemandMaster sand ChargesDemandDetails.
Here when a record is inserted in ChargesDemandMaster then all columns of ChargesDemandDetails are automatically inserted.
The code for this task is as follows:
protected function afterSave()
{
parent::afterSave();
if($this->Demand_fresquency == 'Monthly')
{
for($i=0;$i<12;$i++)
{
$profile = new ChargesDemandDetails;
$profile->Demand_type= $this->DemandType;
$profile->Demand_period = $this->Demand_fresquency;
$profile->Demand_flat_number = 'Not set now';
$profile->Demand_gen_Date = $this->Demand_effective_date;
$profile->Demand_amount = '0000';
$profile->Demand_paid_flag = 'NO';
$profile->Demand_collected_amount = '0000';
$profile->Demand_collected_revenueID = '0000';
$profile->Demand_collectedDate = $this->Demand_effective_date;
$profile->Demand_due = $this->Demand_effective_date;
if (!$profile->save()) {
echo "didn't work";
Yii::app()->end(); // don't use die();
}
}
}
Now in the place of demand_period i need to display Jan, feb , marc…upto december.
I created a table Months(id,month).
i tried this:
for($i=0;$i<12;$i++)
{
$months = new Month;//this is not working
$profile = new ChargesDemandDetails;
$profile->Demand_type= $this->DemandType;
$profile->Demand_period = $months->month; //This is not working
..............................
...........
Please help me in this issue
junxiong
(Junxiong)
May 3, 2011, 11:06am
2
I am not quite sure what you intend to do with the month.
But before you type
$profile->Demand_period = $months->month;
doesn’t you need to set the value first?
e.g:
$months->month = 1; //or 'Jan' maybe?
But then again? Do you intend to store the months name into db? If so, I don’t think this is necessary because
both php and sql know the name of month.
sai_rama
(sai rama)
May 4, 2011, 4:54am
3
junxiong:
I am not quite sure what you intend to do with the month.
But before you type
$profile->Demand_period = $months->month;
doesn’t you need to set the value first?
e.g:
$months->month = 1; //or 'Jan' maybe?
But then again? Do you intend to store the months name into db? If so, I don’t think this is necessary because
both php and sql know the name of month.
Hi junxiong,
thanks for ur response.
I was storing the months{jan,feb…,dec} in this months table.
And then i wanted to get the months respectively.
The problem here i am facing is i am not able to do get access an object of Months here.
$months = new Month;//this is not working .
$profile->Demand_period = $months->month; //This is not working
Here i am writing this aftersave() in ChargesDemandsMaster to automatically populate ChargesDemandsDetails. Now i am not able to get access of Months in ChargesDemandMaster so that Months can also be populated in ChargesDemandDetails
You’ll need to Month::model->findAll() and loop over the record set.
$months = Month::model->findAll();
foreach ($months as $month)
{
$profile = new ChargesDemandDetails;
$profile->Demand_type= $this->DemandType;
$profile->Demand_period = $month->name;
$profile->save();
}
Code not tested.
On a side note, it’s a rather expensive DB call to get month names. You may consider using one of PHPs date functions or writing a helper which returns an array of month names.
Cheers,
Matt
sai_rama
(sai rama)
May 4, 2011, 8:45am
5
waterloomatt:
You’ll need to Month::model->findAll() and loop over the record set.
$months = Month::model->findAll();
foreach ($months as $month)
{
$profile = new ChargesDemandDetails;
$profile->Demand_type= $this->DemandType;
$profile->Demand_period = $month->name;
$profile->save();
}
Code not tested.
On a side note, it’s a rather expensive DB call to get month names. You may consider using one of PHPs date functions or writing a helper which returns an array of month names.
Cheers,
Matt
Hi waterloomatt,
I tried the foreach loop and the error that i am getting is "Trying to get property of non-object"
For the time being i was using an array containing all the values of months.
But the flat_number is again from other model called Apartment. I must get that also.
i dont know where is the mistake.
Thanks for the rely.
Hi,
Not sure what your models look like so I’m guessing on the name. The code I sent you had some errors - I.e. missing brackets.
$months = Month::model->findAll();
should be
$months =Month::model()->findAll();
Also do a count on the number of $months returned to see if your findall is working.
echo Month::model()->count();
If you still have trouble, please send through all relevant models and I will take a look.
Cheers,
Matt
jkofsky
(Jkofsky)
May 4, 2011, 5:47pm
7
What about saving Demand_period as a number then use Yii::app()-locale->getMonthName()
getMonthName() method
public string getMonthName(integer $month, string $width=‘wide’, boolean $standAlone=false)
$month integer month (1-12)
$width string month name width. It can be ‘wide’, ‘abbreviated’ or ‘narrow’.
$standAlone boolean whether the month name should be returned in stand-alone format
{return} string the month name
p.s. Can oneone tell me the difference between standAlone and not?
sai_rama
(sai rama)
May 5, 2011, 11:39am
8
waterloomatt:
Hi,
Not sure what your models look like so I’m guessing on the name. The code I sent you had some errors - I.e. missing brackets.
$months = Month::model->findAll();
should be
$months =Month::model()->findAll();
Also do a count on the number of $months returned to see if your findall is working.
echo Month::model()->count();
If you still have trouble, please send through all relevant models and I will take a look.
Cheers,
Matt
Hi Matt,
I am trying to edit ChargesDemandDetails table from ChargesDemandMaster. In the mean time i also had to enter flat number and months details into chargesDemandDetails.
I was managing Months to be entered statically. But Flat number must be populated dynamically.
The code i wrote in chargesDemandMaster controller is:
protected function afterSave()
{
$a=array('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec');
parent::afterSave();
if($this->Demand_fresquency == 'Monthly')
{
for($i=0;$i<12;$i++)
{
$flatnumbers =Apartment::model()->findAll();
foreach($flatnumbers as $flatnumber)
{ $flatnumber = new Apartment;
$profile = new ChargesDemandDetails;
$profile->Demand_type= $this->DemandType;
$profile->Demand_period = $a[$i];
$profile->Demand_flat_number = $flat_number->flat_number;
$profile->Demand_gen_Date = $this->Demand_effective_date;
$profile->Demand_amount =$this->Demand_amount;
$profile->Demand_paid_flag = 'NO';
$profile->Demand_collected_amount = '0000';
$profile->Demand_collected_revenueID = '0000';
$profile->Demand_collectedDate = $this->Demand_effective_date;
$profile->Demand_due = $this->Demand_effective_date;
if (!$profile->save()) {
echo "didn't work";
Yii::app()->end(); // don't use die();
}
echo Apartment::model()->count();
}
}
}
jayant
(Codesutras)
May 5, 2011, 4:47pm
9
Hi Matt,
I am trying to edit ChargesDemandDetails table from ChargesDemandMaster. In the mean time i also had to enter flat number and months details into chargesDemandDetails.
I was managing Months to be entered statically. But Flat number must be populated dynamically.
The code i wrote in chargesDemandMaster controller is:
protected function afterSave()
{
$a=array('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec');
parent::afterSave();
if($this->Demand_fresquency == 'Monthly')
{
for($i=0;$i<12;$i++)
{
$flatnumbers =Apartment::model()->findAll();
foreach($flatnumbers as $flatnumber)
{ $flatnumber = new Apartment;
$profile = new ChargesDemandDetails;
$profile->Demand_type= $this->DemandType;
$profile->Demand_period = $a[$i];
$profile->Demand_flat_number = $flat_number->flat_number;
$profile->Demand_gen_Date = $this->Demand_effective_date;
$profile->Demand_amount =$this->Demand_amount;
$profile->Demand_paid_flag = 'NO';
$profile->Demand_collected_amount = '0000';
$profile->Demand_collected_revenueID = '0000';
$profile->Demand_collectedDate = $this->Demand_effective_date;
$profile->Demand_due = $this->Demand_effective_date;
if (!$profile->save()) {
echo "didn't work";
Yii::app()->end(); // don't use die();
}
echo Apartment::model()->count();
}
}
}
hi rama you are doing quite right here…just define model attributes properly as you want to save then…then definitely your work have been done…!!