Jump to content

Using a if condition in a model


Iluvatar+

Recommended Posts

If you believe that question is self-explanatory then your answer is "err...no...?"

 

If you're asking "is it possible to have conditional code inside a class but outside the functions in that class" then the PHP interpreter itself could have given you that answer: trying to do that produces a fatal error.

I apologize if i sounded rude, it wasn’t my intention.

 

I have a user m/v/c, in my controller i currently have four action (login, logout, add, edit). Within the edit section i have an image uploaded component in use for the user avatar, however in the add user action i don’t want to have the same component in the form i just want to add content to specific fields in the record. In my model i have numerous parameters set in regards to the upload component, but when i am using the add user action the image uploader parameters seem to be stopping my action from inserting a new record. What i need is a condition that will allow me to run them parameters when necessary (with in the action that uses it - edit action).

 

here is my code anyway...


<?php 
class User extends AppModel {
var $name = 'User';

		# This needs to be used for the image upload functionality.
		var $actsAs = array('Media.Transfer' => array('trustClient' => false,
													  'transferDirectory' => MEDIA_TRANSFER,
													  'createDirectory' => true,
													  'alternativeFile' => 100
													  ),
							'Media.Coupler',
							'Media.Generator' => array(
							'baseDirectory' => MEDIA_TRANSFER,
							'filterDirectory' => MEDIA_USER,
							'createDirectory' => true,
							),
		 );


			// file validation params (which only allowed jpeg and png to be uploaded)
		 var $validate = array(
									'file' => array(
									'mimeType' => array(
									'rule' => array('checkMimeType', false, array( 'image/jpeg', 'image/png'))
			)
			)
		 );


}
?> 

I get

 

"Parse error: syntax error, unexpected T_IF, expecting T_FUNCTION in C:\wamp\www\NEW\cms\app\models\user.php on line 20"

 

<?php 
class User extends AppModel {
var $name = 'User';

var $validate = array(
		'login' => array(
			'rule' => array('_isUnique', 'login'),
			'message' => 'Login name already taken.'
		)
	); 
/*----------------------------------------------------------------------------------------------------------------------	
# Add user premisons model 
var $belongsTo = array('Userpagepremission' => array('className' => 'Userpagepremission',
            											 	  'foreignKey'    => false,
														  'conditions'=> array('Userpagepremission.user_id = User.id')

        )
    );
-----------------------------------------------------------------------------------------------------------------------*/
		if($this->action=='editprofile') {
		# This needs to be used for the image upload functionality.
		var $actsAs = array('Media.Transfer' => array('trustClient' => false,
													  'transferDirectory' => MEDIA_TRANSFER,
													  'createDirectory' => true,
													  'alternativeFile' => 100
													  ),
							'Media.Coupler',
							'Media.Generator' => array(
							'baseDirectory' => MEDIA_TRANSFER,
							'filterDirectory' => MEDIA_USER,
							'createDirectory' => true,
							),
		 );


			// file validation params (which only allowed jpeg and png to be uploaded)
		 var $validate = array(
									'file' => array(
									'mimeType' => array(
									'rule' => array('checkMimeType', false, array( 'image/jpeg', 'image/png'))
			)
			)
		 );
		}

}
?> 

Sorry but your code is unreadable with that kind of indentation.

 

Anyway, you cannot have if statements in the body of a class like that. You'll need to move all of your logic into your __construct() if your plan is to conditionally assign values to an array when the object is created.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.