Skip to content

CI4: Views/attributes/item.php not loading values in dropdown nor current attributes in New/Edit Item form view #3855

Closed

Description

@objecttothis

View > items > form is the popup modal that allows creation of new items and editing existing items.

Starting on line 83 the getAttributes() function in the Items controller is called

		<div id="attributes">
			<script type="text/javascript">
				$('#attributes').load('<?php echo "items/attributes/$item_info->item_id" ?>');
			</script>
		</div>

The problem is that $this->request->getGet() returns null

	public function getAttributes(int $item_id = NEW_ENTRY): void
	{
		$data['item_id'] = $item_id;

		if($this->request->getGet('definition_ids') != null)
		{
			$definition_ids = json_decode($this->request->getGet('definition_ids'), TRUE);
			$data['definition_values'] = $this->attribute->get_attributes_by_item($item_id) + $this->attribute->get_values_by_definitions($definition_ids);
			$data['definition_names'] = $this->attribute->get_definition_names();
		}

getAttributes() then calls the views/attributes/item.php view to display the dropdown and the existing attributes, but since those are all empty, a blank dropdown and no attributes are displayed.

Expected behavior:

  • definition_ids in the get request would not be null
  • on new items a complete dropdown of the available attributes would be output in the view
  • on existing items a dropdown with undefined attributes would be displayed along with existing attributes.

Next Steps:
We need to research why definition_ids in the request is null. It's possible that the problem is in the form view or sooner, so they are never getting populated.

Activity

  1. added this to the 3.4.0 milestone on Nov 3, 2023
  2. self-assigned this
    on Nov 3, 2023
  3. jekkos commented on Nov 4, 2023

    @jekkos
    Member

    I wonder how it worked before, were the definition ids coming from the frontend? Perhaps this was just the contents of the dropdown list sent back (as you need to add an attribute to an item in a first step).

    Also I wonder if all of this just came in through query string parameter, encoded in json? I would need to check the master version to understand.

  4. odiea commented on Nov 4, 2023

    @odiea
    Collaborator

    The following function in Models/Attribute also creates errors. It does not like the & symbol.

    public function get_definitions_by_flags(int $definition_flags): array
    	{
    		$builder = $this->db->table('attribute_definitions');
    		$builder->where('definition_flags &', $definition_flags);
    		$builder->where('deleted', 0);
    		$builder->where('definition_type <>', GROUP);
    		$builder->orderBy('definition_id');
    		$results = $builder->get()->getResultArray();
    
    		return $this->to_array($results, 'definition_id', 'definition_name');
    	}
    
  5. jekkos commented on Nov 5, 2023

    @jekkos
    Member

    Thanks for pointing that out @odiea . The `&= was used as a bit field operator. Maybe the query builder does not support this notation anymore.

  6. objecttothis commented on Nov 5, 2023

    @objecttothis
    MemberAuthor

    @jekkos I think we are talking about this line $builder->where('definition_flags &', $definition_flags);.
    I think that means we want it to produce WHERE 'ospos_definition_flags' &= [definition_flags] where [definition_flags] is the value passed into the function. In PHP the function is defining this as an int, but in the database it's a tinyint(1), so I think this is part of why that was in there. I'll try to debug this tomorrow to see what SQL is actually being produced.

  7. objecttothis commented on Nov 5, 2023

    @objecttothis
    MemberAuthor

    According to the documentation (https://codeigniter.com/user_guide/database/query_builder.html#custom-key-value-method) we may need to include the whole operator &= rather than just part of it.

  8. objecttothis commented on Nov 5, 2023

    @objecttothis
    MemberAuthor

    $builder->where('definition_flags &', $definition_flags); produces WHERE definition_flags & = 1

    Adding the escape parameter $builder->where('definition_flags &', $definition_flags, false); gets rid of the escaping issue, but it still fails. WHERE definition_flags & = 1

    After doing a bit of research, this is because &= is valid SQL, but not valid MySQL. Changing the code to
    $builder->where(new RawSql("definition_flags & $definition_flags"));
    will produce
    WHERE definition_flags & 1
    so that is what we may need to do, but doing so unfortunately doesn't resolve this issue.

  9. objecttothis commented on Nov 5, 2023

    @objecttothis
    MemberAuthor

    Something to be noted about RawSql(), which is new as of CI4.2.0, is that their documentation gives strong warning to escape values and protect identifiers manually. How should we go about doing this? I'm more gun-shy about using esc() since we needed to rip out a bunch of them for having messed up the data.

  10. objecttothis commented on Nov 6, 2023

    @objecttothis
    MemberAuthor

    @SteveIreland in the Items controller you reworked the beginning of the getAttributes() function from this

    		$data['item_id'] = $item_id;
    		$definition_ids = json_decode($this->request->getPost('definition_ids'), TRUE);
    		$data['definition_values'] = $this->attribute->get_attributes_by_item($item_id) + $this->attribute->get_values_by_definitions($definition_ids);
    		$data['definition_names'] = $this->attribute->get_definition_names();
    

    to this

    		if($this->request->getGet('definition_ids') != null)
    		{
    			$definition_ids = json_decode($this->request->getGet('definition_ids'), TRUE);
    			$data['definition_values'] = $this->attribute->get_attributes_by_item($item_id) + $this->attribute->get_values_by_definitions($definition_ids);
    			$data['definition_names'] = $this->attribute->get_definition_names();
    		}
    		else
    		{
    			$data['definition_values'] = [];
    			$data['definition_names'] = [];
    		}
    

    getAttributes() is called from views\items\form.php

    		<div id="attributes">
    			<script type="text/javascript">
    				$('#attributes').load('<?php echo "items/attributes/$item_info->item_id" ?>');
    			</script>
    		</div>
    

    and from views\attributes\items.php

    		var refresh = function() {
    			var definition_id = $("#definition_name option:selected").val();
    			var attribute_values = definition_values();
    			attribute_values[definition_id] = '';
    			$('#attributes').load('<?= "items/attributes/$item_id" ?>', {
    				'definition_ids': JSON.stringify(attribute_values)
    			}, enable_delete);
    		};
    

    The first does not send definition_ids and breaks it so that no attribute values are sent to the views/attributes/item.php view. Is there any reason why I can't change your code to a hybrid of the two?

    		$data['item_id'] = $item_id;
    		$definition_ids = json_decode($this->request->getGet('definition_ids'), TRUE);
    		$data['definition_values'] = $this->attribute->get_attributes_by_item($item_id) + $this->attribute->get_values_by_definitions($definition_ids);
    		$data['definition_names'] = $this->attribute->get_definition_names();
    
  11. objecttothis commented on Nov 6, 2023

    @objecttothis
    MemberAuthor

    After further testing, I think the reason you added the if statement is because passing null in the first parameter of json_decode() is deprecated. I think this can be resolved by using the null coalescing operator ??, so
    $definition_ids = json_decode($this->request->getGet('definition_ids') ?? '', TRUE);

  12. objecttothis commented on Nov 6, 2023

    @objecttothis
    MemberAuthor

    That resolved the problem and I fixed a few other issues. Now onto the next one. updating the attribute_links is failing.

  13. jekkos commented on Nov 6, 2023

    @jekkos
    Member

    great work @objecttothis

  14. odiea commented on Nov 7, 2023

    @odiea
    Collaborator

    @objecttothis . I don't know what is going on with the following error. This is happening when I add or update an Items form. This has been here for a while. Current Ci4-upgrade. Only modification recently was changing the way the form finally displays attributes. Any ideas would be helpful.

    CRITICAL - 2023-11-07 12:34:28 --> Unable to connect to the database.
    Main connection [MySQLi]: Trying to access array offset on value of type bool
    in SYSTEMPATH\Database\BaseConnection.php on line 427.
     1 SYSTEMPATH\Session\Handlers\DatabaseHandler.php(109): CodeIgniter\Database\BaseConnection->initialize()
     2 [internal function]: CodeIgniter\Session\Handlers\DatabaseHandler->open('sessions', 'ospos_session')
     3 SYSTEMPATH\Session\Session.php(928): session_start()
     4 SYSTEMPATH\Session\Session.php(250): CodeIgniter\Session\Session->startSession()
     5 SYSTEMPATH\Config\Services.php(674): CodeIgniter\Session\Session->start()
     6 SYSTEMPATH\Config\BaseService.php(252): CodeIgniter\Config\Services::session(Object(Config\App), false)
     7 SYSTEMPATH\Config\BaseService.php(193): CodeIgniter\Config\BaseService::__callStatic('session', [...])
     8 SYSTEMPATH\Config\Services.php(641): CodeIgniter\Config\BaseService::getSharedInstance('session', null)
     9 SYSTEMPATH\Config\BaseService.php(252): CodeIgniter\Config\Services::session()
    10 SYSTEMPATH\Common.php(979): CodeIgniter\Config\BaseService::__callStatic('session', [])
    11 APPPATH\Models\Employee.php(32): session()
    12 SYSTEMPATH\Config\Factories.php(129): App\Models\Employee->__construct(null)
    13 SYSTEMPATH\Config\Factories.php(86): CodeIgniter\Config\Factories::__callStatic('models', [...])
    14 SYSTEMPATH\Common.php(818): CodeIgniter\Config\Factories::models('Employee', [...], null)
    15 APPPATH\Controllers\Secure_Controller.php(26): model('Employee')
    16 APPPATH\Controllers\Items.php(45): App\Controllers\Secure_Controller->__construct('items')
    17 SYSTEMPATH\CodeIgniter.php(906): App\Controllers\Items->__construct()
    18 SYSTEMPATH\CodeIgniter.php(490): CodeIgniter\CodeIgniter->createController()
    19 SYSTEMPATH\CodeIgniter.php(368): CodeIgniter\CodeIgniter->handleRequest(null, Object(Config\Cache), false)
    20 FCPATH\index.php(67): CodeIgniter\CodeIgniter->run()
    
  15. objecttothis commented on Nov 7, 2023

    @objecttothis
    MemberAuthor

    I've been troubleshooting that error most of the day today. I posted on the code igniter forum. There are problems with the attributes save functions. I suspect that the stack trace is masking another problem.

  16. objecttothis commented on Nov 8, 2023

    @objecttothis
    MemberAuthor

    Here is the forum post. I am working on another project today so if anyone has any ideas, feel free to offer them. https://forum.codeigniter.com/showthread.php?tid=88789

  17. odiea commented on Nov 8, 2023

    @odiea
    Collaborator

    I found the issue. In items form I commented out rules and have not had this problem. It appears that checkNumeric is creating this problem.

  18. objecttothis commented on Nov 9, 2023

    @objecttothis
    MemberAuthor

    OK, I'll play around with checkNumeric. Can you post specifically what you commented out?

  19. odiea commented on Nov 9, 2023

    @odiea
    Collaborator

    App/views/items/form starting at line 576 on my version. Checkitemnumber gave me no issues. I tried adding back individual checks but after 3 to 4 edits it would create the same issue. I also noticed an improvement in the speed of the form updating.

    /*rules:
    			{
    				name: 'required',
    				category: 'required',
    				item_number:
    				{
    					required: false,
    					remote:
    					{
    						url: "<?php echo esc("$controller_name/checkItemNumber") ?>",
    						type: 'POST',
    						data: {
    							'item_id' : "<?php echo $item_info->item_id ?>"
    							// item_number should be passed into the function by default
    						}
    					}
    				},
    				cost_price:
    				{
    					required: true,
    					remote: "<?php echo esc("$controller_name/checkNumeric") ?>"
    				},
    				unit_price:
    				{
    					required: true,
    					remote: "<?php echo esc("$controller_name/checkNumeric") ?>"
    				},
    				<?php
    				foreach($stock_locations as $key=>$location_detail)
    				{
    				?>
    				<?php echo 'quantity_' . $key ?>:
    					{
    						required: true,
    						remote: "<?php echo esc("$controller_name/checkNumeric") ?>"
    					},
    				<?php
    				}
    				?>
    				receiving_quantity:
    				{
    					required: true,
    					remote: "<?php echo esc("$controller_name/checkNumeric") ?>"
    				},
    				reorder_level:
    				{
    					required: true,
    					remote: "<?php echo esc("$controller_name/checkNumeric") ?>"
    				},
    				tax_percent:
    				{
    					required: false,
    					remote: "<?php echo esc("$controller_name/checkNumeric") ?>"
    				}
    			},*/
    
  20. objecttothis commented on Nov 9, 2023

    @objecttothis
    MemberAuthor

    Hmmm. I commented out the rules section, but had the same errors in the log Error connecting to the database: ErrorException: Trying to access array offset on value of type bool. I get those errors when I edit an item and change the value of an attribute. Let's make sure that we are both running the same code though. I can't remember, do you know how to pull down the latest version of a branch using github desktop or another tool? If you need, I can show you how to build a project to make it easier.

  21. odiea commented on Nov 9, 2023

    @odiea
    Collaborator

    Yes I noticed that this issue was still there but it was not preventing the model from doing the update. I use github to download the current versions and work from that. And I merge from the ci4-upgrade branch.

  22. odiea commented on Nov 9, 2023

    @odiea
    Collaborator

    I name the website from who has been doing the current updates.
    2023-11-09 05_55_30-Facebook

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions