I'm trying to save the data from a form. The yii framework form attributes is not being saved in the database.
Here is my code for the controller:
class ProfileController extends Controller {
public function actionSave() {
if (isset($_POST['Patients'])) {
$patients = new Patients;
$patients->name = $_POST['Patients']['name'];
$patients->mobile = $_POST['Patients']['mobile'];
$patients->email = $_POST['Patients']['email'];
$patients->password = $_POST['Patients']['password'];
$patients->dob = $_POST['Patients']['dob'];
$patients->weight = $_POST['Patients']['weight'];
$patients->height = $_POST['Patients']['height'];
$patients->activation_date = '0000-00-00';
$patients->credit = 0;
$patients->archive = 0;
$patients->balance = 0;
$patients->first_login = 0;
$patients->guide_status = 0;
$patients->passcode = '';
}
//echo $patients->save_personal_data();
$valid = $patients->validate();
if ($valid) {
$patients->save();
} else {
var_dump($patients->getErrors());
}
if ($patients->save()) {
echo 'saved';
} else {
var_dump($patients->getErrors());
}
}
It isn't saving in the database. What is wrong with the above code?
It won't save in the DB. Typically the issue is related to the validation rules.
Just for debugging, try using save(false)
//$valid = $patients->validate();
$patients->save(false);
If the model is saved, then check selectively for rules applied to the model.
Related
Is it possible to check when a field is not empty and if not, make an ocg layer visible?
var ocg = FindOCG("Item 1 Arrow");
if (+event.value === '') {
ocg.state = !ocg.state;
} else {
ocg.state = !ocg.state;
}
Something like this (which doesn't work)!
Put this in the custom format script of the field in question. Replace "Square" with the name of your layer. You can see an example of it working here.
function getOCGByName(cName, nPage) {
var ocg = null;
var ocgArray = this.getOCGs(nPage);
for (var i=0; i < ocgArray.length; i++) {
if (ocgArray[i].name == cName) {
ocg = ocgArray[i];
}
}
return ocg;
}
var field = event.target;
var ocg = getOCGByName("Square", this.pageNum);
if (field.value.length > 0) {
ocg.state = true;
}
else {
ocg.state = false;
}
Note: This will only work in Adobe Acrobat and Reader and a few other viewers that are JavaScript capable.
What I have done wrong in this code ? (I am using MVC4 and EF)
As an example: Please clear this am fresher to use MVC4
EditResponse response = new EditResponse();
try
{
using (WeMatchContext db = new WeMatchContext())
{
B_MEMBER_REGISTER update = new B_MEMBER_REGISTER();
var output = db.B_MEMBER_REGISTER.Where(x => x.MEMBER_ID == model.MEMBER_ID).FirstOrDefault();
if(output != null )
{
update.FIRST_NAME = model.FIRST_NAME;
update.LAST_NAME = model.LAST_NAME;
update.GENDER = model.GENDER;
update.DOB = model.DOB;
int resultcount = db.SaveChanges();
if (resultcount > 0)
{
response.MEMBER_ID = update.MEMBER_ID;
response.ResultCode = 0;
response.Message = "Updated Successfully";
}
You have to attach updated data with the db entity. please try this,
using (WeMatchContext db = new WeMatchContext())
{
var update = db.B_MEMBER_REGISTER.Where(x => x.MEMBER_ID == model.MEMBER_ID).FirstOrDefault();
if(update != null )
{
update.FIRST_NAME = model.FIRST_NAME;
update.LAST_NAME = model.LAST_NAME;
update.GENDER = model.GENDER;
update.DOB = model.DOB;
//below line of code is very important.
db.B_MEMBER_REGISTER.Attach(update);
db.Entry(update).State = EntityState.Modified;
int resultcount = db.SaveChanges();
if (resultcount > 0)
{
response.MEMBER_ID = update.MEMBER_ID;
response.ResultCode = 0;
response.Message = "Updated Successfully";
}
}
}
I need help. when i open my index it shows that the nama_unit_kerja variable is not defined..
But i've put them in the SiteController. How can i fix this.
This is my SiteController code :
public function actionIndex()
{
//$this->layout = "//layouts/adr/main";
/* $browser = Yii::app()->browser->isMobile();
echo ($browser?'mobile':'not mobile'); exit; */
#session_start();
Yii::app()->user->returnUrl = array('site/index');
$lang = 'en';
if(isset($_SESSION['lang'])) $lang = $_SESSION['lang'];
// renders the view file 'protected/views/site/index.php'
// using the default layout 'protected/views/layouts/main.php'
$this->layout='//layouts/erp/index';
$modelUnitKerja=new UnitKerja('searchKapTarReal');
$modelUnitKerja->unsetAttributes(); // clear any default values
$modelJenisKegiatan=new JenisKegiatan('searchKapTarReal');
$modelJenisKegiatan->unsetAttributes(); // clear any default values
//var_dump($_POST);exit;
if ((isset($_POST['unitKerja'])) or (isset($_POST['year']))){
if(empty($_POST['unitKerja'])){
$unitKerja = 0;
} else {
$unitKerja = $_POST['unitKerja'];
$nama_unit_kerja = UnitKerja::model()->findByAttributes(array("id"=>$_POST['unitKerja']))->nama_unit_kerja;
}
$year = $_POST['year'];
$year_nm = Year::model()->findByAttributes(array("id"=>$_POST['year']))->year;
}else{
$unitKerja = 0;
$year = Year::model()->findByAttributes(array("year"=>date('Y')))->id;
$year_nm = date('Y');
}
$this->render('index',array(
'mJenisKegiatan'=>$modelJenisKegiatan,
'mUnitKerja'=>$modelUnitKerja,
'unitKerja'=>$unitKerja,
'nama_unit_kerja'=>$nama_unit_kerja,
'year'=>$year,
'year_nm'=>$year_nm,
));
}
$nama_unit_kerja = UnitKerja::model()->findByAttributes(array("id"=>$_POST['unitKerja']))->nama_unit_kerja;
hopefully you are getting this error on this line because you are trying to access the property of yii base.
you can use it like
$nama_unit_kerja = UnitKerja::model()->findByAttributes(array("id"=>$_POST['unitKerja']));
$nama_unit_kerja=$nama_unit_kerja->$nama_unit_kerja;
I tried to research a bit, but has not found a proper solution to this.
What I'm trying to do is to have a HTML.Action in a page like so
Html.Action("Calendar", "GlobalShared")
Inside the action "Calendar", I need to return the result of the html of the traditional calendar control
public ActionResult Calendar(
String ID,
String CssClass,
int CellSpacing,
int CellPadding,
String BorderWidth,
String ShowGridLines,
String ShowTitle,
String DayNameFormat
)
{
System.Web.UI.WebControls.Calendar cal = new System.Web.UI.WebControls.Calendar();
cal.ID = "MyCalendar";
cal.CssClass = "month-view";
cal.CellSpacing = 0;
cal.CellPadding = -1;
cal.BorderWidth = 0;
cal.ShowGridLines = false;
cal.ShowTitle = false;
cal.DayNameFormat = System.Web.UI.WebControls.DayNameFormat.Full;
}
How can I do this? Btw, I use HTML.Action is because I read that it returns a html string, is that correct or should I be doing some other ways?
Thanks
Edit. Before I attempted this in a controller, I tried the following code in a view .cshtml and it works, but I prefer to move the code into a controller
#{
System.Web.UI.WebControls.Calendar cal = new System.Web.UI.WebControls.Calendar();
cal.ID = "MyCalendar";
cal.CssClass = "month-view";
cal.CellSpacing = 0;
cal.CellPadding = -1;
cal.BorderWidth = 0;
cal.ShowGridLines = false;
cal.ShowTitle = false;
cal.DayNameFormat = System.Web.UI.WebControls.DayNameFormat.Full;
cal.RenderControl(new HtmlTextWriter(Html.ViewContext.Writer));
}
Edit #2. The reason I want to use that in a controller is because if in the future, i want to hook up an event say "DayRender", I can do it in the controller. I can not do the same in a view without polluting the view page.
Okay. Thanks guys. I figured out. Basically, I need to use #{Html.RenderAction(..)} and in the action itself, use StringBuilder/StringWriter and then return Content(...). Code below
In View
#{Html.RenderAction("Calendar", "GlobalShared");}
In Controller
[ChildActionOnly]
public ActionResult Calendar(
)
{
System.Web.UI.WebControls.Calendar cal = new System.Web.UI.WebControls.Calendar();
cal.ID = "MyCalendar";
cal.CssClass = "month-view";
cal.CellSpacing = 0;
cal.CellPadding = -1;
cal.BorderWidth = 0;
cal.ShowGridLines = false;
cal.ShowTitle = false;
cal.DayNameFormat = System.Web.UI.WebControls.DayNameFormat.Full;
cal.DayRender += new System.Web.UI.WebControls.DayRenderEventHandler(CalendarDayRender);
StringBuilder sb = new StringBuilder();
using (System.IO.StringWriter sw = new System.IO.StringWriter(sb))
{
System.Web.UI.HtmlTextWriter writer = new System.Web.UI.HtmlTextWriter(sw);
cal.RenderControl(writer);
}
String calHTML = sb.ToString();
return Content(calHTML);
}
private void CalendarDayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
{
e.Cell.Text = "";
if (e.Day.Date == System.DateTime.Today)
{
e.Cell.CssClass = "today";
System.Web.UI.HtmlControls.HtmlGenericControl h3 = new System.Web.UI.HtmlControls.HtmlGenericControl("h3");
h3.InnerHtml = HttpContext.GetGlobalResourceObject("JTG_DateTime", "JTK_Today") + " " + e.Day.DayNumberText;
e.Cell.Controls.Add(h3);
}
}
Simply move your code
#{
System.Web.UI.WebControls.Calendar cal = new System.Web.UI.WebControls.Calendar();
cal.ID = "MyCalendar";
cal.CssClass = "month-view";
cal.CellSpacing = 0;
cal.CellPadding = -1;
cal.BorderWidth = 0;
cal.ShowGridLines = false;
cal.ShowTitle = false;
cal.DayNameFormat = System.Web.UI.WebControls.DayNameFormat.Full;
cal.RenderControl(new HtmlTextWriter(Html.ViewContext.Writer));
}
in a view and return that View from your Calendar Action.
public ActionResult Calendar(
String ID,
String CssClass,
int CellSpacing,
int CellPadding,
String BorderWidth,
String ShowGridLines,
String ShowTitle,
String DayNameFormat
)
{
return View("Calendar");
}
You can either create a ViewModel or use ViewBag to transfer these values from Calendar Action to your newly created View
sendmail.php
This is just sample of attachment.in which i m confused...
class sendmail()
public $ATTACHMENT = array();
public function MailSend()
{
$message = new YiiMailMessage;
$message->subject = $this->SUBJECT;
$message->from = $this->FROM;
$message->setBody($this->EMAILBODY, 'text/html','utf-8');
$message->addTo($this->EMAILTO);
if (count($this->ATTACHMENT) > 0)
{
foreach ($this->ATTACHMENT as $Attachement_Array)
{
if (isset($Attachement_Array['filepath']) && $Attachement_Array['filename'] && $Attachement_Array['mimetype'] && file_exists($Attachement_Array['filepath']))
{
$message->attach(Swift_Attachment::fromPath($Attachement_Array['filepath']), $Attachement_Array['filename'], $Attachement_Array['mimetype']);
}
}
}
}
}
mycontroller.php
here in
$SendMail->ATTACHMENT = array("what to pass here");
please explain me
i want multiple dimensional array of filepath,mimetype,filename
/** SEND EMAIL USING COMMON EMAIL FUNCTIONALITY */
$SendMail = new SendMail;
$SendMail->DRAFT_ID = $id;
$SendMail->SUBJECT = $modelEmail->subject;
$SendMail->FROM = $modelEmail->email_from;
$SendMail->CMPAIGN_ID = 1;
$SendMail->TAG_ARRAY = array("[FIRSTNAME]"=>$profiledata->name,"[EMAIL]"=>$profiledata->email,"[CONTACT_NO]"=>$profiledata->contact_no,"[ADDRESS]"=>$profiledata->address,"[WEBSITE]"=>$profiledata->website);
$SendMail->EMAILTO = $profiledata->name."<".$profiledata->email.">";
$SendMail->ATTACHMENT = array('filepath'=>Yii::app()->params['var_path']."stationary.html"); /// what to write here... i want multidimension array here
$SendMail->MailSend();
/** SEND EMAIL USING COMMON EMAIL FUNCTIONALITY */
my answer is
$attach1 = array();
$attach2 = array();
$attach1 = array('filepath'=>Yii::app()->params['var_path']."stationary.html",
'filename'=>"stationary.html",
'mimetype'=>"application/html",);
$attach2 = array('filepath'=>Yii::app()->params['var_path']."pregmatch1_old.php",
'filename'=>"pregmatch1_old.php",
'mimetype'=>"application/php",);
$SendMail->ATTACHMENT = array($attach1,$attach2);