Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8913775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:32:37+00:00 2026-06-15T04:32:37+00:00

I am working in CakePHP 2.x Currently in my app the user can select

  • 0

I am working in CakePHP 2.x

Currently in my app the user can select to add something as any user. I want to force them to have thier own id as the “user_id”. User_id is a foreign key and i am using ACL, Auth. I have tried to set the data in the controller by using $this->Data => $this->auth->user(‘id); but it doesn’t seam to set the value.

Cakephp Add View:

<?php echo $this->Form->create('Asset'); ?>
<fieldset>
    <legend><?php echo __('Add Asset'); ?></legend>
<?php
    echo $this->Form->input('asset_name');
    echo $this->Form->input('description');
    echo $this->Form->input('vaule');
    echo $this->Form->input('date_bought');
    echo $this->Form->input('date_freehold');
    echo $this->Form->input('user_id');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>

cake php controller:

public function add() {
    if ($this->request->is('post')) {
        $this->Asset->create();
        if ($this->Asset->save($this->request->data)) {
                        $this->data['Assets']['user_id'] = $this->Auth->user('id');
            $this->Session->setFlash(__('The asset has been saved'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The asset could not be saved. Please, try again.'));
        }
    }
    $users = $this->Asset->User->find('list');
    $this->set(compact('users'));
}

Cake Php Model:

class Asset extends AppModel {

/**
 * Validation rules
 *
 * @var array
 */
public $validate = array(
    'asset_name' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'date_bought' => array(
        'date' => array(
            'rule' => array('date'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'user_id' => array(
        'numeric' => array(
            'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
);

//The Associations below have been created with all possible keys, those that are          not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
public $belongsTo = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'user_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);
}

Not 100% sure how or where to do this, any help would be hugely apcreciated thanks guys.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:32:39+00:00Added an answer on June 15, 2026 at 4:32 am

    Here’s how I do it, thought it might be worth posting as it’s a little less code;

    // in AppController
    function beforeFilter() {
        // setup auth here
        ...
        $this->set('authUser', $this->Auth->user());
        ...
    }
    
    // in Add view's form
    ...
    echo $this->Form->hidden('user_id', array('value'=>$authUser['id']));
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a local cakephp 1.2 app which was working fine last night. Currently
I have a cakePHP app working at http://domain1.com/domain2 , and want to point http://domain2.com/
I'm working on a CakePHP project and am currently building the user authentication part
I am working on CakePHP, and I have my app working in my localhost.
I'm currently migrating a working app from CakePHP 1.2 to 1.3. Most things seem
I am currently working on configuring my CakePHP (1.3) based web app to run
I have a CakePHP (latest version) web app with forms and validation all working
I'm working on a CakePHP project and I have User, Post and Location models
I am currently working on an export function in cakephp app and im doing
I'm working on the blog/user auth tutorial for cakephp and trying to add a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.